• 2D
  • Help needed: Making Animated Sprite play once only?

How do I make an AnimatedSprite play once only?

Been making a splash page that fades to black when player dies, and I'm using AnimatedSprite for the fade. However, the node keeps looping/replaying. What should I add on/what am I missing?

extends Node2D

func _ready():
	$AnimatedSprite.play("blackout")

You could use the animation_finished signal and then change/stop the animation when it is finished. Something like this should work (untested):

extends Node2D

func _ready():
	$AnimatedSprite.connect("animation_finished", self, "_on_AnimatedSprite_animation_finished")
	$AnimatedSprite.play("blackout")

func _on_AnimatedSprite_animation_finished():
	if $AnimatedSprite.animation == "blackout":
		$AnimatedSprite.stop()

I think you can set whether an animation loops or not in the editor, but I haven't really used AnimateSprite nodes very much, so I'm not totally sure. It might be something to look into though. In this picture from the Godot documentation, there is a button called Loop, which I think you can click to disable looping for AnimatedSprite nodes:

Regardless, the code above should cause the animation to only play once. Hopefully this helps :smile: