Im trying to make an animation script in GameMaker

84 Views Asked by At

In the script its supposed to run the animation line 6-8 currently and then its supposed to see if down is not pressed and the direction is down then set the sprite to look down Line 23-25 but it keeps playing the animation `x = PlayerObject.x; y = PlayerObject.y;

Direction = "";

if (keyboard_check(vk_down)) {
    Direction = "Down";
    sprite_index = CaseyDownWalk;

}
 else if (keyboard_check(vk_right)) {
    Direction = "Right";
    sprite_index = CaseyRight;
}
else if (keyboard_check(vk_up)) {
    Direction = "Up";
    sprite_index = CaseyUp;
}
else if (keyboard_check(vk_left)) {
    Direction = "Left";
    sprite_index = CaseyLeft;
}
if not (keyboard_check(vk_down)) { 
if (Direction == "Down") {
sprite_index = CaseyDown;
    
    
    
    }
}

i tried changing up the code and what its supposed to do is to play the walking animation then repeat until key not pressed then set the animation to stop

1

There are 1 best solutions below

0
Johannes On

In your code snippet, the line where it says sprite_index = CaseyDown; will never be executed.

In the beginning, you set Direction = "". When the down button is released (and no other button pressed) it will thus still be Direction = "", and the condition if (Direction == "Down") can't be true.

An alternative would be the following: Instead of the direction, you check if you are NOT moving. Then you check which button was released using if keyboard_check_released(vk_down):

moving = false;

if (keyboard_check(vk_down)) {
    moving = true;    
    sprite_index = CaseyDownWalk;
}
 else if (keyboard_check(vk_right)) {
    moving = true;    
    sprite_index = CaseyRight;
}
else if (keyboard_check(vk_up)) {
    moving = true;    
    sprite_index = CaseyUp;
}
else if (keyboard_check(vk_left)) {
    moving = true;    
    sprite_index = CaseyLeft;
}

if (not moving) { 
     if (keyboard_check_released(vk_down)) {
          sprite_index = CaseyDown;   
    }
}