Beginner Help with animation

Godot Version

extends Area2D

@onready var game_manager = %GameManager

@onready var collectible = $AnimatedSprite2D

func _on_body_entered(body):

if (body.name == "CharacterBody2D"):
	collectible.animation = "collect"
	game_manager.add_point()
	queue_free()

Question

When I run the game it simply does not play the animation. Any Idea why this is?

Try collectible.play(“collect”)

The object is being deleted as soon as it is collected with queue_free(). You will need to add some logic to keep it around for the animation while preventing it from being collected twice.

var collected: bool = false

func _on_body_enetered(body) -> void:
    if collected == false and body is CharacterBody2D:
        collectible.play("collect")
        game_manager.add_point()

        collected = true
        await collectible.animation_finished
        queue_free()

You need to have an await in there for the animation time because it’s jumping straight to queue free. I can’t tell you the exact code but yeah, if you look at the documentation for await or animation player it should be there.

It might even be in the timer section.

Probably something like await.get_tree().create_timer(animation duration).start_timer()

Or await.animationplayer.finished

Hey,

figured I should be thorough for you. here’s the code.

await $AnimationPlayer.animation_finished

or in your specific case:

await collectible.animation_finished

Thank you for the Input from all of you. The problem was the the collectible was disappearing before the animation could play!