This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

I have a fireball that should switch to a new explode animation upon contact with a physics body. Is there a way to play an animation using the body_entered function?

I would have figured with the below code the fireball would just pass through objects and play the new animation?

I've tested queue_free and the fireball correctly interacts with bodies and gets removed from memory, but I can't get the animation to change.

func _on_Fireball_body_entered(body):
    $AnimatedSprite.play("explo")
    # queue_free()
in Engine by (13 points)

2 Answers

+2 votes

If you call queue_free() right after you start your new animation, the animation won't show because your fireball will be removed pretty much instantly. Keep in mind that while queue_free() does not immediately delete an object, it will be deleted in the next frame, so at 60 fps it will be gone in about 16.6 milliseconds.

You could do something like

func _on_Fireball_body_entered(body):
    $AnimatedSprite.play("explo")
    yield($AnimatedSprite, "animation_finished")
    queue_free()

This will pause the body entered function until the animation has finished (signal animation_finished is emitted by AnimatedSprite and then free the fireball (the code above is untested, but you get the idea :) ).

For more info on yield, see the docs.

by (1,519 points)
+1 vote

podrías probar con esto

func _on_Fireball_body_entered(body): 
    if body == player:
    $AnimatedSprite.playing = true
    $Timer.start()
func _on_timer_timeout():
    queue_free()

ajustando en tiempo según cuando acabe la animación

by (16 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.