• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

How to make an enemy ignore the player when not facing them, and change states while using a timer.

R

Rew711

Guest
I'm pretty sure I might need to add some type of true/false statement but for now I'm kinda at a point where I don't know what to do. I'm hoping that someone can help because I've looked at online tutorials and they only talk about the full area around the enemy rather than just a specific point.

I'm also having trouble getting the enemy AI to go from what Case to another. I think it might have something to do with having a timer running in the code.

Step Event:

Code:
//animation
frame_counter();

//state switch
switch currentState 
{   
    case states.move:
        if not instance_exists(oPlayer) break;
       
        var distance_to_player = point_distance(x, y, oPlayer.x, oPlayer.y);
       
        sprite = Frog_Hop;
        last_image = (image_speed - 1);
        image_speed = 0.1;
       
        xPos = sign(oPlayer.x - x);
       
        move_state(self.dir, self.movespeed);
       
        if (image_index > last_image) and (distance_to_player < 128)
        {
            image_speed = 0;
            pauseTimer = -1;
            movespeed = 0;
           
            currentState = states.attack;
        }
        if (image_index > last_image) and !(distance_to_player < 128)
        {
            image_speed = 0;
            pauseTimer ++;
            movespeed = 6.5;
            if (pauseTimer >= Time_For_Jump) 
            {
                image_index = 0;
                movespeed = 0;
                puaseTimer = 0;
            }
            if (pauseTimer = 40)
            {
                pauseTimer = 0;
            }
        }
    break;
   
    case states.attack:
        if not instance_exists(oPlayer) break;
       
        sprite = Frog_Attack;
       
        var distance_to_player = point_distance(x, y, oPlayer.x, oPlayer.y);
       
        xPos = sign(oPlayer.x - x);
        dir = xPos;
       
        movespeed = 0;

        if !(image_index > sprite_get_number(sprite_index) - 1)
        {
            xPos = 1;
            pauseTimer = -1;
            currentState = states.attack;
        }
        else if    (image_index > sprite_get_number(sprite_index) - 1)
        {
            xPos = -1;
            pauseTimer = 1;
            currentState = states.normal;
        }
       
    break;
   
    case states.hit:
        hit_state();
    break;
}

//reset frame to 0 if sprite changes
if(lastSprite != sprite)
{
    lastSprite = sprite;
    frame = 0;
}
Begin Step:

Code:
onGround = check_below();

if(onGround)
{
    ySpeed = 0;
}
else
{
    ySpeed += gSpeed;
}
End Step:

Code:
xPos = x;
yPos = y;

//animation
frame_reset();

//hurtbox
with(hurtbox)
{
    x = other.x + xOffset;
    y = other.y + yOffset;
}

//get hit
if !(hit)
{
    currentState = states.move
}
else if(hit)
{
    squash_stretch(1.3,1.3);
    xSpeed = hitBy.xHit;
    hitStun = hitBy.hitStun;
    facing = hitBy.owner.facing * -1;
    hit = false;
    currentState = states.hit;
    dir *= -1;
    facing = dir;
    pauseTimer = 0;
    image_index = 0;
}

//hitbox
if(hitbox != -1)
{
    with(hitbox)
    {
        x = other.x + xOffset;
        y = other.y + yOffset;
    }
}

///collision
var h, v;

// Handle sub-pixel movement
xCounter += xSpeed;
yCounter += ySpeed;
h = round(xCounter);
v = round(yCounter);
xCounter -= h;
yCounter -= v;

repeat(abs(v))
{
    if(ySpeed <= 0)
    {
        if(place_meeting(x,y+sign(v),oWall))
        {
            ySpeed = 0;
            break;
        }
        else
        {
            y += sign(v);
        }
    }
    else 
    if (check_below())
    {
        ySpeed = 0;
        break;
    }
    else
    {
        y += sign(v);
    }
}

repeat(abs(h))
{
    if(place_meeting(x+sign(h),y,oWall))
    {
            xSpeed = 0;
            dir *= -1;
            facing = dir;
            break;
    }
    else
    {
        x += sign(h);
    }
}
Its pretty simple code and its a mix of code from different sources but its mostly from a user who had previously helped me on this site. And most of the other code is from a guy who showed you a bit of code through an Amazon page tutorial but he never really finished.

I tried a lot of things with the code, moving each thing around and/or adding in something new but it always seems to do either nothing or just crash the game.

I hope that whatever it is, I can learn from it for future coding purposes.
 

TheouAegis

Member
last_image = (image_speed - 1)
You mean last_image=image_number-1; which is kinda pointless to do since it's just as easy to type image_number-1 wherever you have last_image.

Typically to check if the enemy is facing the player, you can do

if sign(oPlayer.x-x) == sign(image_xscale);

Assuming of course that you use image_xscale to look left and right.

Edit: looking at your code, it looks like it's basically

if xpos != dir break;
 
R

Rew711

Guest
You mean last_image=image_number-1; which is kinda pointless to do since it's just as easy to type image_number-1 wherever you have last_image.

Typically to check if the enemy is facing the player, you can do

if sign(oPlayer.x-x) == sign(image_xscale);

Assuming of course that you use image_xscale to look left and right.

Edit: looking at your code, it looks like it's basically

if xpos != dir break;
I see what your saying. And I'm a little embarrassed I couldn't remember/think it up. Thank you. But, I still have issues getting the code to switch from the move state to the attack state. I think I had this same issue with another enemy I've made but ignored it for now. Is there something wrong with my code that prevents it from changing states?

Edit

I forgot to mention that the "last_image = (image_speed - 1)" is there so I can have the enemy hopping. If I were to change it to number, it wouldn't work the same way.
 
Last edited by a moderator:
Top