How do you stop AnimatedSprite2D once animation is down?

Godot version 4.1

I have my animated sprites playing perfectly fine, but they won’t stop, and I want them to stop as soon as the animation is done.

2024-04-26-191359_355x201_scrot

Click this button to disable looping

1 Like

Thanks, but how do I add code to do something when the animation is finished so that the animation can return to the idle animation?

Connect to the animation_finished signal, you may have to compare against the animation name property if you are looking for a specific animation

I tried doing this but couldn’t get it working. Could you explain it clearer please?

Could you explain further what you tried?

This is what the script would look like if it handled the connection itself, though you can also connect signals from the Node tab by the Inspector in-editor.

extends Node2D

func _ready() -> void:
	$AnimatedSprite2D.animation_finished.connect(_on_animation_finished)


func _on_animation_finished() -> void:
	if $AnimatedSprite2D.animation == "default":
		print("done animating!")

I know practically nothing about connections and whatnot, so I don’t understand how any of what you sent me works

What do I put in the default? I get an “invalid opera da object and string in operator ==

Signals, and connecting them is very important! Read through this document. Maybe also get started on the Dodge the Creeps tutorial game, which covers a lot of the basics.

1 Like

“default” is the name of the animation, by default it is “default” the sample code was messed up because animation is part of the $AnimatedSprite2D and I forgot to include that.

I guess I just don’t understand how to use animated sprite 2D stuff. When I connect the animation finished thing, how does it know WHICH animation I want to be finished is finished? Not much about animatedsprite2d stuff in code makes sense to me.

So I have an attack animation, and when that’s done, I want it to return to the idle animation, could you please explain every step so that I get it right? And if you use placeholder names, please put what should be in the placeholder.

The script I posted before works like so:

  1. func _ready() runs when the scene is created, it connects _on_animation_finished to the animation_finished signal
  2. _on_animation_finished now runs when any animation finishes
  3. Using if $AnimatedSprite2D.animation == "default": the following code will only run if the finished animation is “default”, it sounds like you want to replace “default” with “attack”
  4. prints “Done animating!” but of course you could change this to play another animation via $AnimatedSprite2D.play("idle")

What does the → void mean

the function return type, in this case void means it does not return anything.

func integer_num() -> int:
    return 4

func decimal_num() -> float:
    return 4.56

func no_return_type() -> void:
    print("my int: ", integer_num())
    print("my float: ", decimal_num())
    # see no return!

It is part of the optional type system which I recommend using for better autocomplete and error handling.


I tried it, but this happened:

_ray_gun is the variable for my $AnimatedSprite2D

I also can’t move my character with this code for some reason

It appears the error is gone, but I still can’t move, and it doesn’t work.

it seems like _ray_gun isn’t defined yet, could you paste this script in it’s entirety?

Also move_and_slide() being where it is seems like you’ve pasted code into the middle of your movement processing.

1 Like

Move and slide is fixed, somehow I didn’t know that’s how it worked lol. The error is gone, but it still doesn’t work. I’ll replace everything with _ray_guns actual thing and see if that works