Reverse sprite animation and change sprite_index

M

Magander86

Guest
Hello everyone, this is my first thread and time in the community. :)

I'm having trouble with my ship animation. It's a prototype top down shooter.
My trouble is...
The movement of the ship is made by pressing WASD keys. When I press left or right (A or D), the animation that leans to that directions are doing OK. To make a more fluid animation, when the respective key is released, I want the sprite animation to reverse until it gets to the first frame of the sub-image and then it changes to another sprite_index to an idle animation.

So, here is the code

Code:
//Input
//animation right
if(keyboard_check(ord("D"))){
    x += walkSpeed;
    sprite_index = spr_player_right;
    image_speed = 1;
    if(image_index > image_number -1)
        image_speed = 0;   
}
//reverse animation when the key is released
if(keyboard_check_released(ord("D")))
{
    image_speed = -1;
    if(image_index == 1)
        sprite_index = spr_player;
    
}
When I release the key, the animation loops in reverse. I think it doesn't pass through the "if" condition.

Idle animation
4eb95649-a769-40c8-bab3-d34edca0c6ce.png
Lean to the right animation
985436b8-4f42-46fd-a086-1f8da490bf73.png 358cb6e5-b977-4d86-b901-778c0f278923.png
 

TheouAegis

Member
image_speed = 1
if image_number-1 {

Pretty sure that doesn't work. If you have a sprite with 5 frames and the image_index is 4, it adds 1 after the Step event. At that point image_index is 5 and that's rounded down to 0. ..Or maybe it does work.

Either way, the if image_index==1 line is only checked ONE TIME and that's when you release the D key. You need to instead set a variable when the D key is released and run the code if that variable is set. Be sure to clear that variable after you've switched back to the normal sprite.
 
M

Magander86

Guest
Thanks for your answer.
Now I understand that the release_key function works once. So, the way you explained it I couldn't figure out how to put this in code.
What I did was a variable that decrement the value of the image_index as the code below:

Code:
if(keyboard_check_released(ord("D")))
{
    img_spd = -1;
    image_index += img_spd;   
    if(image_index <= 0)
        sprite_index = spr_player;
    
}
If you show me an example code how to make this function run the condition so the sprite changes, I would be much obliged.
I've tried a while loop, but it freezes the game.
 
J

JFitch

Guest
1. The image_index can never be less than 0.
2. You're testing the image index only at the time the key is released.
Try this:
Code:
if(keyboard_check_released(ord("D")))
{
   image_speed = -1;
}
if (image_speed==-1 && image_index<1)
{
   sprite_index = spr_player;
   image_speed = 1;
}
 
M

Magander86

Guest
Thanks man!
It is now clear for me, I was in a wrong mindset. Now it works.
 
Top