how can I stop an animation when it touches the ground (is_on_floor ())

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

My character has an animation when gravity affects him (“speed.y > 0”). but before I touch the ground, I want to press the down key in the air so that when I get to the ground, the sitting animation is done, but this animation does not happen, on the contrary the animation of when the gravity affects it is frozen.

thats my scrips:


func _move(delta):
if velocidad.y > 0:
	$AnimatedSprite.play("Caer")
if is_on_floor():
   if Input.is_action_just_pressed("ui_down"):
		$AnimatedSprite.play("Sentandose")
		yield ($AnimatedSprite, "animation_finished")
		$AnimatedSprite.play("Sentado")
:bust_in_silhouette: Reply From: Brinux

Perhaps instead of calling a false keypress, a better way would be to let the animation play by tracking the ‘in air’ state of your player. Maybe like this:

var was_in_air == false #previous frame in-air state
var in_air == false  #current frame in-air state

func _move(delta):
    if velocidad.y > 0:
        $AnimatedSprite.play("Caer")
        in_air = true
func is_on_floor():
    if was_in_air == false && in_air == true:  #will only trigger once
        $AnimatedSprite.play("Sentandose")
        yield ($AnimatedSprite, "animation_finished")
        $AnimatedSprite.play("Sentado")
        in_air = false
was_in_air = in_air

    

thank you so mucho my friend <3

skylabelmho | 2019-08-16 20:26