0 votes


In my 2D platformer I want a stopping-animation when my character finishes running. The problem is, it stops playing when I "override" it with new animations like walk and idle.
Is there a way my code can detect which animation is currently playing and also when it finished?

Something like

if current animation name = stopping: wait for animation to finish
else: play idle

Sorry, no actual code, I'm a beginner in this. I'm using animated sprites. Thanks!

in Engine by (23 points)
edited by

1 Answer

+2 votes
Best answer

Take a look at AnimatedSprite. You can check if given animation is currently playing, you can stop and start or check which frame is actually playing (that can be used to blend animations).
What you can do:
Check for input, if player pressed Right Key, stop idle animation, start walking animation. If player released Right Key, stop walking animation, start idle animation.


Click on AnimatedSprite inside scene tree, in properties click on created Frames so it will open Animation Frames interface. Create animations there, name will be used in code (play(animation name) or animation == animation_name). Throw there all frames that are related to given animation and done.

by (378 points)
selected by

Thanks for your effort and all, but we seem to talk past each other, I've already created plenty of animations using the AnimatedSprite.
But you kind of helped me :-)

I've now found out, that checking works with this

if $Sprite.animation == "run":
 #do something

The last thing I need is to let the animation finish or check, if it has been finished.
So how do I use "animation_finished()"?

Thanks again.

Just use docs that I linked above. It's all there, you know the amount of frames for each animation so why not check if current frame for given animation is equal to number of frames.

if $Sprite.animation == "run" && $Sprite.frame == $Sprite.frames.get_frame_count("run"):
    # Animation "run" finished

No offence, but as a beginner I find the docs difficult to understand. For me it is mostly "raw data" with no examples how to use it properly.
Your example looks good, but it does not work (at least in my test nothing happened), so I did it like this and it worked, so thank you.

if $Sprite.animation == "run" && $Sprite.frame == 6:

get_frame_count may return total number of frames. If there is frame 0-5 then get_frame_count will return 6. So try that:

if $Sprite.animation == "run" && $Sprite.frame == $Sprite.frames.get_frame_count("run")-1:
    # Animation "run" finished

It's easier that way, you don't have to hardcode numbers or even remember them.
Not tested, just how I think it is.

Well, that worked! Thank you very much!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.