Enemy flash doesn't reset instantly when hitted

Godot Version(stable)

4.3

Question(visuals) :bangbang:

i added a feature that if you hit the enemy a flash animation will play.but if you spam the animation resets when the animation ended instead of reseting instantly.btw here is my code:

extends CharacterBody2D

@export var SPEED : int = 200
var dir : float = 1
var health : int = 35
@onready var anim : AnimatedSprite2D = $AnimatedSprite2D
@onready var ray_cast_right: RayCast2D = $RayCastRight
@onready var ray_cast_left: RayCast2D = $RayCastLeft
@onready var player: CharacterBody2D = $“.”

func _ready() → void:
$DamageZone.queue_redraw()

func _process(delta: float) → void:
if health != 0:
anim.play(“walk”)
position.x += dir * SPEED * delta
anim.show()

	if ray_cast_left.is_colliding():
		dir = 1
		anim.flip_h = false
	if ray_cast_right.is_colliding():
		dir = -1
		anim.flip_h = true

func _physics_process(delta: float) → void:
if not is_on_floor():
velocity += get_gravity() * delta
move_and_slide()

func _on_damage_zone_area_entered(area: Area2D) → void:
if area.name == “Bullet”:
if health > 0:
health -= 1
$AnimationPlayer.play(“hit_flash”)
if health == 0:
explode()

func explode() → void:
anim.hide()
$DamageZone.queue_free()
$CPUParticles2D.emitting = true
await get_tree().create_timer(1.3).timeout
queue_free()

func _on_visible_on_screen_notifier_2d_screen_exited() → void:
hide()

func _on_visible_on_screen_notifier_2d_screen_entered() → void:
show()

if you know the answer, pls reply :speech_balloon: i will really appreciate it :smiley:

That’s because if you use AnimationPlayer.play("animation_a") while this node is already playing “animation_a” the call will be simply ignored. You need to stop the animation first to reset the animation position and after call your animation again:

$AnimationPlayer.stop()
$AnimationPlayer.play(“hit_flash”)

still the same.i really dont understand why it didn’t work.this is what i coded:

func _on_damage_zone_area_entered(area: Area2D) → void:
if area.name == “Bullet”:
if health > 0:
health -= 1
$AnimationPlayer.stop()
$AnimationPlayer.play(“hit_flash”)
if health == 0:
explode()

Do a test and put this in your code and check if your code print “bullet hit” for every bullet you shot:

func _on_damage_zone_area_entered(area: Area2D) → void:
	if area.name == “Bullet”:
		print("Bullet hit") # Add this line in your code
		if health > 0:
			health -= 1
			$AnimationPlayer.stop()
			$AnimationPlayer.play(“hit_flash”)

		if health == 0:
			explode()

you’re right.there is just something wrong with the collisions.THX

My guess is the way you’re checking the collision, keep in mind if you add a scene called “Bullet” one time, this name will keep as is, but if you add this scene again while the first node still in scene, Godot will rename your node to @Bullet2@ because you can’t have the same name in the same parent node, instead, add your bullet node to a group with add_to_group("bullet") and check if the node that collided is the correct with if area.is_in_group("bullet"):

its not a collision error.its another thing.i dont know what,but the collisions are correct

i fixed it by myself.it was because the bullet plays an explosion animation.and the animation takes 0.3 seconds,i puted now a timer instead of await and everything works fine,THX!