0 votes

Hi,

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.

Edit

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 reply.
But how do I use "isplaying" and "animationfinished"?

I tried this:

onready var spritenode= get_node("Sprite")
if spritenode.is_playing():
    print("animation playing")

This works, but where do I put the name of the animation, so I can check if "walk" is the current animation?

if spritenode.animation_finished():
    print("animation finished")

This doesn't work. I get "Nonexistant function".

Unfortunately I found the docs not very helpful so far, because there are no examples on how to properly use most things in actual code.

If you look at Member Variables you will see:

String animation - The current animation from the frames resource. If this value changes, the frame counter is reset.

I hope you are using AnimationPlayer which is explained in Introduction to the 2D animation features. That way you can check which animation is active (using animation from above) and if it's currently playing.

Unfortunately I'm using AnimatedSprite right now. Does this mean I have to switch to the AnimationPlayer then? Is this not possible or difficult with AnimatedSprite?

That will work too, if you are using AnimatedSprite then SpriteFrames are way to go.

Ok, but how do I use these member functions?

I just want the code in my character (KinematicBody2D) to check, if a certain animation is currently active and if so, do something. Like finishing it before playing the next animation.

Do I use this with func?

func has_animation(walk)
 print("walking")

Nothing happening so far.

Or like this?

func _process(delta):
 if $Sprite.has_animation("walk"):
  print("walking")

Also nothing happening.

Check my edit.

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:
 #Something

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.

Categories