Stop (or Pause) and restart animation of a 2dAnimatedSprite

Godot Version

Question

I have an 2dAnimatedSprite (of a dice) that rolls based on a random number genetator, it seems to work well and i want to pause it for a few seconds, none of these metods seems to work to stop and resume the animation

$“AnimationPlayer”.stop(true)
$“AnimationPlayer”.stop()
$“AnimationPlayer”.pause(true)
$“AnimationPlayer”.pause()

I’m pretty new to Godot, any ideas on why it does no work, or what metod should I use to pause the animation for a couple seconds and restart it on demand (like a button or a timer)

thanks!

$"AnimationPlayer".pause() # Don't pass any argument, it doesn't need them
# Waiting to resume...
$"AnimationPlayer".play() # Don't specify any argument to resume

I don’t know why is this not working for you, but this should be the solution.

If you’re using an AnimatedSprite2D you don’t need an AnimationPlayer.
I mean, you could have it, but you don’t need it.
Just do this:

@onready var animated_sprite_2d = $AnimatedSprite2D
func _process(delta):
	if Input.is_action_just_pressed("ui_left"):
		animated_sprite_2d.pause()
	if Input.is_action_just_pressed("ui_right"):
		animated_sprite_2d.play()

I just assigned left/right arrow to pause and unpause, but you get the idea. Just reference the AnimatedSprite2D directly, you don’t need an AnimationPlayer. The whole point of an AnimatedSprite2D is that you don’t use AnimationPlayer.

1 Like

Here’s how an AnimatedSprite2D works (without AnimationPlayer).

The only problem is that is a fixed animation. If you want it to look more “random” you’d have to change the property “frame” of the AnimatedSprite2D by code.

1 Like

And here is an example changing the property “frame” every time. This is not an animation, this is a manual change of the property “frame”, you don’t even need to use “play” or “pause”, just changing the “frame” index is enough.
I press the space bar to pause and unpause.

1 Like

My friend javier.8 this worked just perfect, thanks a lot!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.