• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

GMS 2 Idle animation code doesn't work properly

E.M.I

Member
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.M.I

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.
Worked perfectly, thanks a lot!
 
Top