How to play a animation when pressing a key

[size=150]Hey, I am new to Gdevelop and i need help trying to figure out how to play an animation when pressing a key
I wanna make an attack button such as “D” that will play the attack animation, i got the jumping and running and falling but when i put the when pressing d play animation 4 ( which is the attack animation) all i get is the animation playing 1 frame and not all of them. So if someone could help me that would be AWESOME :sunglasses: [/size]

Have you done:

Change the animation of object to 4
Play the animation of object


Also, is the time between frames appropriate? You can see the time between frames by clicking on the little clock icon just above the animation numbers list. It is set by default to 1s, which is often far too slow.

Without an event-screenshot I’m just guessing, but probably the events before the attack-animation event are (almost allways) changing the animation too, for example:

Conditions: Player is on the ground Actions: Set animation = 0 (stand)

Conditions: Key "d" is pressed Actions: Set animation = 4 (attack)
The problem here: If you have the “d” key pressed, GD will change the animation to 4, but in the next frame (just 0.01667 seconds after) the first event is True, so the animation is set to = 0 (and reset), and then is set to = 4 (and reset). The result is that you see the first frame of the 4th animation after every frame :slight_smile:

You should have a system to avoid this, for example a state-holder: If the “d” key is pressed, set animation to 4 and set “state” = “attacking”. In my first event: If Player is in the ground and “state” = “none”. Finally you should add an event that check if the animation 4 has ended, and set “state” = “none”:

Conditions: Player is on the ground, Variable "state" = "none" Actions: Set animation = 0 (stand)

Conditions: Key "d" is pressed, Trigger once? Actions: Set animation = 4 (attack), Set variable "state" = "attack"

Conditions: Animation of Player is = 4 (or variable "state" = "attack"), Animation has finished Actions: Set variable "state" = "none"

1 Like

[size=150]Hi again, i still was not able to figure out how to play more then one frame in the animation, i do got the animation timer set right so its not playing a frame every second, but when i press the key i set it to it only will play one frame, i was able to get a picture of what my script page looks like so i got the link here, [/size]

You are always changing the animation in the first 3 events. If you want the attack animation to play correctly, you must stop to change the animation to 0, 1, 2 or 3 in the previous events (you can test the content of the State variable for example)

And please, stop posting with a big font size.

Sorry maybe im just dull today but i dont know what you mean? , i thought i did change that animation to play when pressing d ? ( only worked with rpgmaker before so thats why im still noob to programing with Gdevelop)

You are setting the animation 4 when d is pressed but you are also setting the animation to 0 because the ninjaja is on the floor and is not moving (event 3.2).

YEAH!!! ok lol i finally know what you mean when i looked over it, I SAW IT!!! im making it revert back to floor animation, once i got rid of it just to see what would happen i could play the whole animation SO THANKS A TON!!! :laughing:

I need some assistance on this

So the animation plays like I want it to but now the walk animation doesn’t work unthill I use the attack after that jumping animation and walking work again. also how to make it so you cant move while doing attack animation?

help is appreciated

OK so now I got it doing the move animation again but now I cant do the attack animation without moving left or right

This is probably more complicated then it should be but iv been here for a couple hours now just trying to get it to work correctly.

I think I’ve found your problem…

delete the line 6, since I think it’s not necesarry
use “at the beginning of the scene” instead and for the action “make your state variable to none”

that’s all… just correctly if I am wrong :blush: :slight_smile:

Ah that worked but now if I jump while attacking jump animation gets stuck anyway to disable a key press like C which I have as jump while attack animation is playing?