• 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!

Legacy GM [SOLVED] sprite_index not working with variable

S

suikun

Guest
Been trying to figure this one out for a while. Any help would be appreciated! :)

I have an NPC in a top down-style game that moves in four directions: up, right, down, left. I'm using the following code to change the sprite depending on the direction the NPC is walking. However, the NPC occasionally stops too. In these cases, I want the sprite's animation to stop moving, but I want it to still be facing the correct direction. My idea was to get the sprite direction via a variable, last_sprite. The animation stops as expected but the sprite always changes to face right rather than the last/current direction.

This is in Draw Begin:

GML:
move_direction = (point_direction(xprevious,yprevious,x,y))

if (move_direction <= 45) or (move_direction > 315)
{
    sprite_index = npc_r; //right
    last_sprite = npc_r;
}

if (move_direction <= 135) and (move_direction > 45)
{
    sprite_index = npc_u; //up
    last_sprite = npc_u;
}

if (move_direction <= 225) and (move_direction > 135)
{
    sprite_index = npc_l; //left
    last_sprite = npc_l;
}

if (move_direction <= 315) and (move_direction > 225)
{
    sprite_index = npc_d; //down
    last_sprite = npc_d;
}

if x == xprevious && y == yprevious
{
    sprite_index = last_sprite;
    image_index = 1;
}
Also, don't know if this is helpful: There are multiple instances of this object/NPC in a room. GameMaker Professional 1.4.9

Any ideas on what I'm doing wrong here?
 

TsukaYuriko

☄️
Forum Staff
Moderator
point_direction will point to the right (0) when pointing from and to the same point... therefore that's where it will turn.

Add an additional check to see if the NPC is moving at all. Only update the facing direction if it is.
 
S

suikun

Guest
THANK YOU, TsukaYuriko! It worked. I changed the code to the following:

GML:
move_direction = (point_direction(xprevious,yprevious,x,y))

if (speed > 0) {
    if (move_direction <= 45) or (move_direction > 315)
    {
        sprite_index = npc_r; //right
        last_sprite = npc_r;
    }
    
    if (move_direction <= 135) and (move_direction > 45)
    {
        sprite_index = npc_u; //up
        last_sprite = npc_u;
    }
    
    if (move_direction <= 225) and (move_direction > 135)
    {
        sprite_index = npc_l; //left
        last_sprite = npc_l;
    }
    
    if (move_direction <= 315) and (move_direction > 225)
    {
        sprite_index = npc_d; //down
        last_sprite = npc_d;
    }
}

if x == xprevious && y == yprevious
{
    sprite_index = last_sprite;
    image_index = 1;
}
Also added this to the create event:
Code:
last_sprite = npc_d;
 
Top