Damaged animation not playing

Godot Version

4.6.2

Question

Ok so my player has all the animations down ok, but when I want it to play “damaged” it does not play. I’m just really stuck here and am not sure what to change to fix it.

here is the character body 2d code for the player character:

extends CharacterBody2D
class_name Player
signal damaged

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

@onready var animated_sprite_2d = $Sprite2D as PlayerAnimatedSprite
@onready var area_collision_shape = $"Area2D/area collision shape"
@onready var body_collision_shape = $bodycollisionshape
@onready var area_2d = $Area2D
@onready var Rescue = Rescuables

var run_speed_damping = 0.6
var speed:float = 300.0
var jump_velocity = -425

var collected: bool = false
var knockback: Vector2 = Vector2.ZERO
var knockback_timer: float = 0.0



@onready var min_stomp_degree = 35
@onready var max_stomp_degree = 145
@onready var stomp_y_velocity = -150
@onready var bounceback = -300
@onready var bouncebackx = -150


@onready var camera_sync: Camera2D = $Camera2D2
@onready var should_camera_sync: bool = true




func _physics_process(delta):
	
	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_released("jump") and velocity.y < 0:
		velocity.y *= 0.5
		
	var direction = Input.get_axis("left","right")

	if direction:
		velocity.x = lerpf(velocity.x, speed * direction, run_speed_damping * delta)
	else:
		velocity.x = move_toward(velocity.x, 0, speed * delta)
	
	animated_sprite_2d.trigger_animation(velocity, direction)
	
	move_and_slide()
	
func _on_area_2d_area_entered(area):
	if area is Enemy:
		handle_enemy_collision(area)
		print("lalal")
	if area is Block:
		handle_block_collision(area)


func handle_block_collision(block: Block):
	if block == Block:
		die()
	
func handle_boss_collection(boss: Boss):
	if boss == null:
		return
	var angle_of_collision = rad_to_deg(position.angle_to_point(boss.position))
	if angle_of_collision > min_stomp_degree && max_stomp_degree > angle_of_collision:
		boss.damaged()
	
func handle_enemy_collision(enemy: Enemy):
	if enemy == null:
		return
	var angle_of_collision = rad_to_deg(position.angle_to_point(enemy.position))
	
	if angle_of_collision > min_stomp_degree && max_stomp_degree > angle_of_collision:
		enemy.die()
		on_enemy_stomped()
	elif GameManager.health > 1:
		GameManager.health -= 1
		damaged.emit()
		velocity.y = bounceback
		velocity.x = bouncebackx
		animated_sprite_2d.play("damage")
	else:
		die()
		

func on_enemy_stomped():
	velocity.y = stomp_y_velocity
	
func die():
	animated_sprite_2d.play("die")
	area_2d.set_collision_mask_value(3, false)
	set_collision_layer_value(1, false)
	set_physics_process(false)
	
	var death_tween = get_tree().create_tween()
	death_tween.tween_property(self, "position", position + Vector2(0, -48), .5)
	death_tween.chain().tween_property(self, "position", position + Vector2(0,256),1)
	death_tween.tween_callback(func (): get_tree().reload_current_scene())
	await get_tree().create_timer(1.2).timeout
	GameManager.health = 3
	
func _process(_delta):
	if global_position.x > camera_sync.global_position.x && should_camera_sync:
		camera_sync.global_position.x = global_position.x
	if global_position.x < camera_sync.global_position.x && should_camera_sync:
		camera_sync.global_position.x = global_position.x


func _on_brick_2_body_entered(_body: Node2D):
	die()

and here is the code for the animated sprite 2:

extends AnimatedSprite2D

class_name PlayerAnimatedSprite
@onready var player = $".."

func ready ():
	player.damaged.connect(update)
	update()
	
	if Input.is_action_pressed("jump"):
		play("jump1")
	
func update():
	play("damage")
	

func trigger_animation(velocity: Vector2, direction: int):
	
	
	if sign(velocity.x) != sign(direction) && velocity.x != 0 && direction != 0:
		play("idle")
		
	else:
		if scale.x == 1 && sign(velocity.x) == -1:
			scale.x = -1
		elif scale.x == -1 && sign(velocity.x) == 1:
			scale.x = 1
			
	if velocity.x != 0:
			play("walking")
	if velocity.y != 0:
		play("jump1")
	else:
			play("idle")

My guess is this is a magic string error. In this case, you’re calling all the functions “damaged” and yet when you call the animation you use “damage” (without a ‘d’). The best way to fix this is not use Strings in your code, but instead use constants. For example, in your second script:

class_name PlayerAnimatedSprite extends AnimatedSprite2D

const JUMP_ACTION = "jump"
const JUMP_ANIMATION = "jump1"
const IDLE_ANIMATION = "idle"
const WALK_ANIMATION = "walking"
const DAMAGED_ANIMATION = "damaged" # Note the 'd' added at the end.

@onready var player = $".."


func ready ():
	player.damaged.connect(update)
	update()
	
	if Input.is_action_pressed(JUMP_ACTION):
		play(JUMP_ANIMATION)

	
func update():
	play(DAMAGED_ANIMATION)
	

func trigger_animation(velocity: Vector2, direction: int):
	if sign(velocity.x) != sign(direction) && velocity.x != 0 && direction != 0:
		play(IDLE_ANIMATION)
	else:
		if scale.x == 1 && sign(velocity.x) == -1:
			scale.x = -1
		elif scale.x == -1 && sign(velocity.x) == 1:
			scale.x = 1
			
	if velocity.x != 0:
			play(WALK_ANIMATION)
	if velocity.y != 0:
		play(JUMP_ANIMATION)
	else:
		play(IDLE_ANIMATION)

By replacing all your strings with constants, you make it easy to change the name in one place for your file, and you can make it clearer with the constant names what the string stands for. Making them constants also makes them memory use smaller and execution faster.