i'm trying to add an attack animation, but it keeps getting cut off by the idle and run animation

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Thijs406

Hello. I have recently started to code with godot. But I keep running into one issue: when I try to add a attack animation with:

if Input.is_action_just_pressed("attack"):
	$AnimationPlayer.play("AttackBalltrum")

(Balltrum is my player)

When I play the scene and use my mouse to attack, it gets cancelled by my idle and run animation, so my attack is just one fast frame.

extends Actor

func _physics_process(delta: float) → void:
var is_jump_interrupted: = Input.is_action_just_released(“jump”) and _velocity.y < 0.0
var direction: = get_direction()
_velocity = calculate_move_velocity(_velocity, direction, speed, is_jump_interrupted)
_velocity = move_and_slide(_velocity, FLOOR_NORMAL)

var facing_right = _velocity.x > 0.0
var facing_left = _velocity.x < 0.0

if facing_right == true:  
	$Balltrum.scale.x = 1.5
	$CollisionShape2D.position.x = 4
if facing_left == true:
	$Balltrum.scale.x = -1.5
	$CollisionShape2D.position.x = -4

if Input.is_action_pressed("move_right"):
	facing_right = true
	$AnimationPlayer.play("RunBalltrum")
elif Input.is_action_pressed("move_left"):
	facing_left = true
	$AnimationPlayer.play("RunBalltrum")
else:
	_velocity.x = lerp(_velocity.x,0,0.2)
	$AnimationPlayer.play("IdleBalltrum")


if _velocity.y > 0.0 and is_on_floor() == false:
	$AnimationPlayer.play("JumpDownBalltrum")

if Input.is_action_just_pressed("attack"):
	$AnimationPlayer.play("AttackBalltrum")

func get_direction() → Vector2:
return Vector2(
Input.get_action_strength(“move_right”) - Input.get_action_strength(“move_left”),
-1.0 if Input.is_action_just_pressed(“jump”) and is_on_floor() else 1.0
)

func calculate_move_velocity(
linear_velocity: Vector2,
direction: Vector2,
speed: Vector2,
is_jump_interrupted: bool
) → Vector2:
var out: = linear_velocity
out.x = speed.x * direction.x
out.y += gravity * get_physics_process_delta_time()
if direction.y == -1.0:
out.y = speed.y * direction.y
if is_jump_interrupted:
out.y = 0.0
return out

:bust_in_silhouette: Reply From: SQBX

Before playing the animation, you can call $AnimationPlayer.is_playing() to check if an animation is only being played.

If you care only if a specific animation is playing, call:

$AnimationPlayer.is_playing() and $AnimationPlayer.current_animation == "example_anim"`

Also, you should probably make an onready variable at the top of your script referencing the AnimationPlayer so you don’t have to call get_node aka $ every time you need it:

onready var animation_player = $AnimationPlayer