how do i implement the attack animation in my code

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

how do i implement the attack animation?
when i try to implement the attack animation never shows correctly, it start for a split second then stops…
Here’s the code:

extends KinematicBody2D
class_name Player

export(Resource) var PlayerMovement

var velocity = Vector2.ZERO
var Buffered_Jump = false
var is_attacking = false

onready var Jump_Buffer_Timer: = $jumpBufferTimer
onready var remoteTransform2D: = $RemoteTransform2D

func _physics_process(_delta):
	
	var input = Vector2.ZERO
	input.x = Input.get_action_strength("right") - Input.get_action_strength("left")
	move_state(input)

func move_state(input):

	apply_gravity()
	if input.x == 0:
		apply_friction()
		$AnimatedSprite.play("idle")
	else:
		apply_acceleration(input.x)
		if is_on_floor():
			$AnimatedSprite.play("run")
		if input.x > 0:
			$AnimatedSprite.flip_h = false
		elif input.x < 0:
			$AnimatedSprite.flip_h = true
	
	attack()
	
	if is_on_floor():
		reset_double_jump()
	if can_jump():
		jump_input()
	else:
		$AnimatedSprite.play("jump")
		input_jump_realese()
		input_double_jump()
		buffered_jump()
		fast_fall()
		
	
	velocity = move_and_slide(velocity, Vector2.UP)
	
	
func player_die():

	SoundPlayer.play_sound(SoundPlayer.HURT)
	queue_free()
	Events.emit_signal("player_died")

func fast_fall():

	if velocity.y > 5:
			$AnimatedSprite.play("fall")
			velocity.y += PlayerMovement.FALL_GRAVITY
	
func buffered_jump():

	if Input.is_action_just_pressed("jump"):
		Buffered_Jump = true
		Jump_Buffer_Timer.start()
		
func input_double_jump():

	if Input.is_action_just_pressed("jump") and PlayerMovement.DOUBLE_JUMP > 0:
		SoundPlayer.play_sound(SoundPlayer.JUMP)
		velocity.y = PlayerMovement.JUMP_FORCE - PlayerMovement.DOUBLE_JUMP_FORCE
		PlayerMovement.DOUBLE_JUMP  -= 1
	
func input_jump_realese():

	if Input.is_action_just_released("jump") and velocity.y < -30:
		velocity.y = PlayerMovement.JUMP_RELEASE_FORCE
	
func can_jump():

	return is_on_floor()
	
func reset_double_jump():

	PlayerMovement.DOUBLE_JUMP = PlayerMovement.DOUBLE_JUMP_COUNT
	
func jump_input():

	if Input.is_action_just_pressed("jump") or Buffered_Jump:
		SoundPlayer.play_sound(SoundPlayer.JUMP)
		velocity.y = PlayerMovement.JUMP_FORCE
		Buffered_Jump = false
	
func apply_gravity(): 

	velocity.y += PlayerMovement.GRAVITY
	velocity.y = min(velocity.y, 500)
	
func apply_friction():

	velocity.x = move_toward(velocity.x, 0, PlayerMovement.FRICTION)

func apply_acceleration(amount):

	velocity.x = move_toward(velocity.x, PlayerMovement.MAX_SPEED  * amount, PlayerMovement.ACCELERATION)

func _on_jumpBufferTimer_timeout():

	Buffered_Jump = false

func connect_camera(camera):

	var camera_path = camera.get_path()
	remoteTransform2D.remote_path = camera_path
	
func attack():

	if Input.is_action_just_pressed("attack"):
		is_attacking = true
		$AnimatedSprite.play("attack")

Edited to fix code formatting. In the future, when adding code to a post 1) paste the code 2) select the code block, 3) press the {} button to format it for the forum, and 4) verify everything looks good via the Preview panel prior to submitting.

jgodfrey | 2023-02-12 20:18

:bust_in_silhouette: Reply From: jgodfrey

Assuming all of the logic is correct to call you attack() function as needed, I’d guess the problem is that you’re probably continually “restarting” the animation from the first frame (due to simply calling play() on each call. To fix it, you need to make the attack() function a little smarter so it doesn’t restart an already playing animation on each call.

Something like this might get you close…

func attack():
    if Input.is_action_just_pressed("attack"):
        is_attacking = true
        if !$AnimatedSprite.playing or $AnimateSprite.animation != "attack":
            $AnimatedSprite.play("attack")

Completely untested, but that should only start a new attack animation if no animation is playing, or the current animate is not the attack animation…

i made some changes

extends KinematicBody2D
class_name Player

export(Resource) var PlayerMovement

var velocity = Vector2.ZERO
var Buffered_Jump = false
var is_attacking = false


onready var Jump_Buffer_Timer: = $jumpBufferTimer
onready var remoteTransform2D: = $RemoteTransform2D

func _physics_process(_delta):
	
	var input = Vector2.ZERO
	input.x = Input.get_action_strength("right") - Input.get_action_strength("left")
	move_state(input)

func move_state(input):
	apply_gravity()
	if input.x == 0 and is_attacking == false:
		apply_friction()
		$AnimatedSprite.play("idle")
	else:
		apply_acceleration(input.x)
		if is_on_floor() and is_attacking == false:
			$AnimatedSprite.play("run")
		if input.x > 0:
			$AnimatedSprite.flip_h = false
		elif input.x < 0:
			$AnimatedSprite.flip_h = true
	
	attack()
	
	if is_on_floor():
		reset_double_jump()
	if can_jump():
		jump_input()
	else:
		if is_attacking == false:
			$AnimatedSprite.play("jump")
			input_jump_realese()
			input_double_jump()
			buffered_jump()
			fast_fall()
		
	
	velocity = move_and_slide(velocity, Vector2.UP)

...
func attack():
	if Input.is_action_just_pressed("attack"):
	   is_attacking = true
       $AnimatedSprite.play("attack")

func _on_AnimatedSprite_animation_finished():
	if $AnimatedSprite.animation == "attack":
			is_attacking=false

And now it works!
ty for the help

Hyakkimaru | 2023-02-13 01:14