How can i change an animation from attack back to idle?

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

Hy, i don’t know how to change the animation back to idle when the attack anim has finished. Here is the code:

func _physics_process(delta):
motion.y+=GRAVITY

if Input.is_action_pressed("ui_right"):
	motion.x=SPEED
	$AnimatedSprite.flip_h=false
	if is_on_floor():
		$AnimatedSprite.play("walk")
elif Input.is_action_pressed("ui_left"):
	motion.x=-SPEED
	$AnimatedSprite.flip_h=true
	if is_on_floor():
		$AnimatedSprite.play("walk")
elif is_on_floor():
	motion.x=0
	$AnimatedSprite.play("idle")
	
if is_on_floor() and Input.is_action_just_pressed("ui_up"):
	motion.y=JUMP
	
if is_on_floor() and Input.is_action_just_pressed("attack"):
	$AnimatedSprite.play("attack1")

if !is_on_floor():
	$AnimatedSprite.play("jump")

motion=move_and_slide(motion,UP)

Did you solve the problem?

BrunoFreezee | 2018-06-15 18:15

:bust_in_silhouette: Reply From: bitwes

AnimationPlayer has a ‘finished’ signal you can connect to. It passes the animation that finished. You can check that and kick off a different animation.

:bust_in_silhouette: Reply From: Socrates

Create a variable that refers to the string name of the current animation. Connect the AnimatedSprite’s animation_finished signal to a function:

$AnimatedSprite.connect("animation_finished", self, "name_of_function")

The function can check if the variable == “attack1” and then change it to “idle” if so. This requires replacing the string literals referring to the animations with the variable, which will be changed whenever you want to change animations.

:bust_in_silhouette: Reply From: KND2501

Below the walk inputs you can place…

else:
	motion.x = 0
	if is_on_floor():
		$AnimatedSprite.play("idle")