Attack animation not workin

Godot Version

Version 4

Question

Whenever i hit the key for attack, the animation doesn’t play. Here’s my Code:

extends CharacterBody2D

const SPEED = 100.0
const JUMP_VELOCITY = -300.0

Get the gravity from the project settings to be synced with RigidBody nodes.

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

@onready var animated_sprite = $AnimatedSprite2D

func _process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta

# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = JUMP_VELOCITY

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("move_left", "move_right")

if direction > 0:
	animated_sprite.flip_h = false
elif direction < 0:
	animated_sprite.flip_h = true


if is_on_floor():
		if direction == 0:
			animated_sprite.play("idle")
		else:
			animated_sprite.play("run")
else:
	animated_sprite.play("jump")

if Input.is_action_pressed("attack"):
	animated_sprite.play("attack")

if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()