Is there a way to keep count of the loops of an animated sprite?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dondudding

Want I’m looking to do is set a variable to the the number of times an animated sprite has looped and then use that as a signal to do something. For example, every time the animation loops, it adds one to a variable, and then when the variable reaches 10, it calls a function to do something.

Anyone know an easy line of code to do this?


:bust_in_silhouette: Reply From: jgodfrey

I can think of a few ways that would probably work…

If the sprite is being animated via an AnimationPlayer, you can add a call method track at the end of the animation timeline. That’ll call a function you define every time the animation reaches the end.

In that function, you could manage your counter and/or decide to stop the repeating animation and call your mentioned “done” function.

Alternatively, you could set the animation to not loop (so, to only play once). Then, you could hook up the animation’s animation_finished signal. In the code called by that signal, you could manage your “counter” variable. If you haven’t yet reached the maximum number of plays, just play the animation again. Otherwise, call your “done” function.

That’s just what I was looking for. Thanks so much.

dondudding | 2020-02-09 18:45