Death animation or explosion?

Godot Version

Godot V4,3

Question

` How to play death or explosion animation (GIF) right
Before enemy dies - queue_free()
?

Make sure the animation doesn’t loop. Then wait for the animation’s animation_finished signal to fire. Once the signal has fired, you can use queue_free().

1 Like

so how will that be in code?

I have this for bullet script

extends Area2D
@onready var death: AnimatedSprite2D = $Sprite2d/death


func _physics_process(delta):
	position += transform.x * speed * delta


func _on_body_entered(body: Node2D) -> void:
	if body.has_method("enemy_hit"):
		body.enemy_hit()
		queue_free()

and this for enemy
script

extends CharacterBody2D

@onready var death: AnimatedSprite2D = $Sprite2d/death

func enemy_hit():
	health -= 1
	if health == 0:
		death.animation = "death"
		queue_free()

but it did not work maybe I’m doing it wrong

Something like:

extends CharacterBody2D

@onready var death: AnimatedSprite2D = $Sprite2d/death

func _ready()->void:
	death.animation_finished.connect(_on_animation_finished.bind())

func enemy_hit():
	health -= 1
	if health == 0:
		death.animation = "death"
		
func _on_animation_finished()
	queue_free()
1 Like

the enemy exploded and disappeared right away after adding this code and after I hit play/enter game I didn’t even shoot him lol

Wait, is this:

the enemy script in its entirety? Because I kind of assumed the snippet was just the relevant bits. If you only only have one animation and that animation autoplays when the scene is instantiated, then that would explain the behavior you see.

The code should be changed to account for the enemy’s state. (i.e. idle, walking, running, dying, etc.)

2 Likes

I dont know but… i think you could do:

func _on_body_entered(body: Node2D) -> void:
If body.has_method("enemy_hit"):
    Body.enemy_hit()
    Animatedsprite.play("death")
    get_tree().create_timer(1).timeout
    Queue_free()


#you can ofcourse adjust the time however you like instead of making it 1 like what i did here
1 Like

same thing with Animatedsprite.play

I didnt know it was a sprite, i thought it was an animated sprite, my bad, i dont think theres a thing called “play()” in the Sprite node, i think its only in animatedsprite2d and animationplayer

1 Like

I tried this it worked for a second after I shoot the enemy like 10 times I can see the explosion for a second then it crashes right away and I get this error

1 Like

instantiate() rather than instance() for PackedScene, I think; see line 44.

1 Like

now when I changed instance() to instantiate() the explosion disappeared but no crash!

Maybe try:

    explosion.position = global_position

instead of explosion.global_position. It might not make a difference, but…

At the worst, maybe temporarily attach a script to the explosion scene and print out things like when it gets an update and where it thinks it is…

1 Like

The Explosion plays alone or all the time, without hitting and killing the enemy why is that I want the explosion to appear only after the death of the enemy
I tried
enabling one shoot still explosion play alone without killing the enemy

script for explosion

extends CPUParticles2D


func _ready():
	emitting = true
	
func _process(delta):
	if emitting == false:
		queue_free()

script for enemy

func enemy_hit():
	health -= 1
	if health == 0:
		queue_free()
		
	if health == 0:
		var explosion = Explosion.instantiate()
		explosion.position = global_position
		get_tree().current_scene.add_child(explosion)