Animation Override/Prioritization Issue

Godot Version 4.2.2 stable

Hello, I am brand new to Godot and game development in general.

Objective: I am trying to trigger an animation I named “hurt” whenever my character collides with an enemy.

Problem: The animation does not appear to be playing. I believe it is being triggered, because when I debug it with a print statement, it appears in the console upon collision with another body. I think it may be conflicting with my “run” and “idle” animations, i.e., it is triggered, but instantly being overridden by those animations.

Question: How do I pause animations to make way for others? Or prioritize/override them?

With my jump, idle, and run animations, they just work automatically, they seem to just override each other with no problem.

Code:
I have a scene handling character health and damage named “take_damage”. This script calls the method called “play_hurt_animation” in my characters script.

take_damage script:

> extends Area2D
> 
> var max_health = 2
> var current_health = 1
> @onready var timer = $Timer

> func _on_body_entered(body):
> 	print("ouch")
> 	max_health -= current_health
> 	if max_health < 2:
> 		print(max_health)
> 	if max_health < 1: 
> 		body.get_node("CollisionShape2D").queue_free()
> 		timer.start()
> 
> 	if body.has_method("play_hurt_animation"):
> 		body.play_hurt_animation()
> 
> func _on_timer_timeout():
> 	death()
> 
> func death():
> 	get_tree().reload_current_scene()

////BREAK////

character_body_2d script:

> extends CharacterBody2D
> 
> const SPEED = 120.0
> const JUMP_VELOCITY = -300.0
> 
> @onready var animated_sprite_2d = $AnimatedSprite2D
> @onready var take_damage = $TakeDamage
> 
> var direction_flip = 1
> 
> var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
> 
> func play_hurt_animation():
> 		animated_sprite_2d.play("hurt")
> 		
> func _physics_process(delta):
> 	# Add the gravity.
> 	if not is_on_floor():
> 		velocity.y += gravity * delta
> 	if Input.is_action_just_pressed("jump") and is_on_floor():
> 		velocity.y = JUMP_VELOCITY
> 	if Input.is_action_just_pressed("move_left"):
> 		direction_flip = -1
> 		animated_sprite_2d.flip_h = true
> 	if Input.is_action_just_pressed("move_right"):
> 		animated_sprite_2d.flip_h = false
> 	
> 	var direction = Input.get_axis("move_left", "move_right")
> 	
> 	if is_on_floor():
> 		if direction == 0:
> 			animated_sprite_2d.play("idle")
> 		else:
> 			animated_sprite_2d.play("run")
> 	else:
> 		animated_sprite_2d.play("jump")
> 	
> 	if direction:
> 		velocity.x = direction * SPEED
> 	else:
> 		velocity.x = move_toward(velocity.x, 0, SPEED)
> 		
> 	move_and_slide()

If you want your hurt animation to be prioritized on top of any other animation then you’ll need to check if the hurt animation is playing before playing another animation. I’ve not tested it but maybe something like this:

var next_animation = ""
if is_on_floor():
	if direction == 0:
		next_animation = "idle"
	else:
		next_animation = "run"
else:
	next_animation = "jump"
	
	
if not next_animation.is_empty() and not animated_sprite_2d.animation == "hurt" and not animated_sprite_2d.is_playing():
	animated_sprite_2d.play(next_animation)