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!
/// @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!