Animation player runs many times

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

Hello this code executes an animation when it is detected that the player has touched a floor

if get_slide_count() != 0:
		var body = get_slide_collision(get_slide_count()-1)
		if body.normal == Vector2(0,-1) and body.collider.is_in_group("puas"):

The problem is that since the player is constantly touching the floor, the animation runs many times.
How would I make him

:bust_in_silhouette: Reply From: RedBlueCarrots

Change the last 2 lines to:

if body.normal == Vector2(0,-1) and body.collider.is_in_group("puas") and get_node("../Puas/anim".is_playing():

The extra is_playing() check will make it so that the animationplayer will only restart if it is not currently playing.

Hope this helps

PS: I fixed the formatting in the answer. For the record, you need to separate the code sample from the rest of the text with one blank line above and below, and indent all lines with 4 spaces. (Other intendation should add 4 spaces on top of that, for every indentation level.)

Calinou | 2020-05-11 08:50

Oh thanks for the heads up, I’m pretty new on these forums so that is good to know!

RedBlueCarrots | 2020-05-11 10:08

Also note that your answer is fixing a non-existent problem: “If the animation was already playing, it will keep playing.” (Source)

njamster | 2020-05-11 13:53

:bust_in_silhouette: Reply From: njamster

If you want the animation to only play once, use a boolean:

var triggered = false

# ...

if not triggered:
    triggered = true