Problem with attack animation GMS2 (SOLVED)

KPJ

Member
Hi everyone. I have an issue with setting up the animations in my game (I am using Gamemaker Studio 2). I am creating a top-down game, and so far I have three player sprites: Idle, Walk and Stealth Attack.

The Player's Idle and Walk sprites are working correctly (when the player is moving, the sprite is set to Walk, and when it's not moving, it is set to Idle)

However, the Stealth Attack animation is not working. I want it to be so that when the player presses the space bar, the sprite will change to the Attack sprite (which has 5 frames).

When I press the space bar, it only plays the first frame of my Attack animation, then it goes back to the sprite it was before. (If my sprite is Idle and i press space bar, it will only play the first frame of the Attack animation before going back to Idle)

Does anyone know whats wrong? Thanks!

Code:
Code:
//Player Step Event
if (keyboard_check(ord("W"))) || (keyboard_check(ord("A"))) || (keyboard_check(ord("S"))) || (keyboard_check(ord("D")))
{
    sprite_index = sPlayerWalk;
}

else
{
    sprite_index = sPlayerIdle;
}

if (keyboard_check_pressed(vk_space))
{
    image_speed = 1;
    sprite_index = sPlayerStealthAttack;
}
Code:
//Player Animation End
if (sprite_index == sPlayerStealthAttack)
{
    sprite_index = sPlayerIdle;
}
 

NightFrost

Member
It is in how you've written your code. On the following step after you've pressed space, the code checks if player has pressed any of WASD. If yes, switch to walk sprite; otherwise switch to idle sprite. The general solution to persisting specific states across multiple steps is to structure your code into a state machine. You should be able to easily find GMS-specific tutorials for that.
 
  • Like
Reactions: KPJ

KPJ

Member
It is in how you've written your code. On the following step after you've pressed space, the code checks if player has pressed any of WASD. If yes, switch to walk sprite; otherwise switch to idle sprite. The general solution to persisting specific states across multiple steps is to structure your code into a state machine. You should be able to easily find GMS-specific tutorials for that.
Thanks for the reply! I will definitely try looking into state machines.
Just out of curiousity, what are the other ways to work around this issue other than state machines?
 

KPJ

Member
Well, I looked more into state machines and implemented them into my game. The issue was fixed! Thank you NightFrost!
 
Top