Problem with playing animation

v 4.2.2

Question

Hello, I’m trying to make my first game in Godot. I wan’t to trigger an attack animation with my script, but the animation doesn’t play properly and get’s stuck at the first frame or just flickers a little. Here is my script:

extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0

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

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
var isRunning = false
var isAttacking = false
var isAttackingRun = false
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta

# Handle jump.
if Input.is_action_just_pressed("ui_accept") 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("ui_left", "ui_right")
if direction:
	velocity.x = direction * SPEED
	
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
	

if direction > 0:
	%AnimatedSprite2D.flip_h = false
if direction < 0:
	%AnimatedSprite2D.flip_h = true	
if is_on_floor():
	if direction == 0:
		%AnimatedSprite2D.play_idle()
		isRunning = false
	else:
		%AnimatedSprite2D.play_run()
		isRunning = true	
		
if Input.is_action_just_pressed("attack"):
	isAttacking = true
	if  %AnimatedSprite2D.animation != "attack":
			%AnimatedSprite2D.play("attack")
move_and_slide()	

func attack():
isAttacking = true
if %AnimatedSprite2D.animation != “attack”:
%AnimatedSprite2D.play(“attack”)

the attack animation must get over shadowed by the constant calls to play_idle() and/or play_run(). You must either make players not move while attacking or just don’t play a new animation during attack.

It works! Thanks!