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

GameMaker Idle animation code doesn't work properly

E

E.M.I

Guest
Hey everyone! I'm starting to work in GMS2 and ran into a bit of trouble while writing code to make the player change to the appropriate idle sprite depending on the last direction they walked to. As you can see in this video the character always uses the down idle sprite (https://cdn.discordapp.com/attachments/565286316003033110/565286409318039563/LostMyth.mp4) Here's the code:


/// @description Allows the player to move

var w_key = keyboard_check(ord("W"));
var d_key = keyboard_check(ord("D"));
var s_key = keyboard_check(ord("S"));
var a_key = keyboard_check(ord("A"));

direction_idle = 3;

if (w_key)
{
phy_position_y -= spd;
sprite_index = spr_eitim_walk_up;
image_speed = 0.7;
direction_idle = 1
}

if (d_key)
{
phy_position_x += spd;
sprite_index = spr_eitim_walk_right;
image_speed = 0.7;
direction_idle = 2
}

if (s_key)
{
phy_position_y += spd;
sprite_index = spr_eitim_walk_down;
image_speed = 0.7;
direction_idle = 3;
}

if (a_key)
{
phy_position_x -= spd;
sprite_index = spr_eitim_walk_left;
image_speed = 0.7;
direction_idle = 4;
}

//Stop the character from animating when they stop walking

if (!w_key and direction_idle == 1)
{
image_speed = 0;
sprite_index = spr_eitim_idle_up;
}

if (!s_key and direction_idle == 3)
{
image_speed = 0;
sprite_index = spr_eitim_idle_down;
}

if (!d_key and direction_idle == 2)
{
image_speed = 0;
sprite_index = spr_eitim_idle_right;
}

if (!a_key and direction_idle == 4)
{
image_speed = 0;
sprite_index = spr_eitim_idle_left;
}

Any idea on how to fix this? Thanks a lot!
 

YoSniper

Member
You keep resetting direction_idle to 3 at the start of each step. If the player is not holding any keys, this value will not get overwritten. I expect that if you take out the line
Code:
direction_idle = 3;
then everything will run fine.
 
E

E.M.I

Guest
You keep resetting direction_idle to 3 at the start of each step. If the player is not holding any keys, this value will not get overwritten. I expect that if you take out the line
Code:
direction_idle = 3;
then everything will run fine.
Worked perfectly, thanks a lot!
 
Top