Help with overlapping animations with animatedsprites2D

Godot version 4.2.1

I’m new to Godot and GDscript and had just added animations to my little player character, I’ve got a run, jump, idle and attacking animations but I cannot figure out a way to keep my other animations from overriding my attacking animation. anytime there’s another animation going on, the attacking animation cannot start and is frozen at the first frame. This feels really rudimentary but I cant seem to figure it out.
here’s my code and all help is appreciated.

extends CharacterBody2D

@onready var _animations = $AnimatedSprite2D

const SPEED = 200.0
const JUMP_VELOCITY = -400.0

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

func _physics_process(delta):
if not is_on_floor():
	velocity.y += gravity * delta"Jump")

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

#Left and right movement and animations
var direction_R = Input.get_action_strength("right")
var direction_L = Input.get_action_strength("left")
if direction_R:
	velocity.x = direction_R * SPEED
	_animations.flip_h = false"Run")
elif direction_L:
	velocity.x = direction_L * -SPEED
	_animations.flip_h = true"Run")
	velocity.x = move_toward(velocity.x, 0, SPEED)"Idle")
#An Attempt with an attacking animation.
var mouseButton_L = Input.get_action_strength("Mousebutton1")
if mouseButton_L:"Attack")


With this code, attack animation would only play when Mousebutton1 input is pressed and player is on the floor. If that’s the desired behavior, I would instead use is_action_pressed since mouse buttons can only be pressed or not pressed.

A few things you can clean up for more predictable behavior:

  1. There’s no ‘return’ statement in the case of playing “Jump” animation - it seems that if player is jumping, they can’t also be running. If that’s correct, add “return” after“Jump”)

  2. Since you’re using the same animated sprite for all animations, I assume you don’t want both running and attacking animations to play at the same time. Otherwise, you need to use multiple AnimatedSprite2Ds or AnimationPlayers.

  3. You send same “play()” command to your _animations object 60 times a second. While AnimatedSprite2D may not restart the animation on every “play()” call, generally when you call same method before it’s finished executing, it’s a clue that your code is missing state, i.e. knowing what is currently happening. The State Machine design pattern solves this and also allows you to code predictable behavior that’s far easier to debug (after you overcome the initial complexity of implementing the pattern, of course). You may benefit from looking up a tutorial on State Machine pattern implementation.

Last but not least, ofc make sure you’ve properly setup the Attack animation since you need to manually indicate frames for every new animation on the AnimationSprite2D afaik.

Hope this is helpful, cheers!

Use two AnimationPlayer nodes, one for each animation. This allows you to have full control over both animations independently.