hi, im making a game, and im using animatedsprites to play the animations, but, when i press a key, i have to hold the same to play all the animation. i want when i press a key, play all the animation, how i do this? look at my code to run the animation:

func _fixed_process(delta): is_act = Input.is_action_pressed("kick") func anim_player(): var animatedSprite = get_node("AnimatedSprite") if(is_act): if(player_direction == RIGHT): animatedSprite.play("kick_right") else: animatedSprite.play("kick_left")

I think the problem is in your 'if' check. Something like this should work:

func _fixed_process(delta):
   var animatedSprite = get_node("AnimatedSprite")
   var is_playing_animation = animatedSprite.is_playing();
   is_act = Input.is_action_pressed("kick")

    if(is_act and !is_playing_animation):
      if(player_direction == RIGHT):
        animatedSprite.play("kick_right")
      else:
        animatedSprite.play("kick_left")

I think the reason your code is not working is because you're checking if the key is down and then you play an animation, but you're not checking if a animation is already playing, so the animation gets reset.

didnt work, i think i forgot to say that have a animation that run when the player stop, look at all of my code

extends Node2D const PLAYER_WALK_SPEED = 180 const PLAYER_RUN_SPEED = 200 const RIGHT = Vector2(1, 0) const LEFT = Vector2(-1, 0)

var player_speed = PLAYER_WALK_SPEED var player_direction = Vector2() var can_move = true var is_moving = false var is_act = false

func _ready(): set_fixed_process(true)

func _fixed_process(delta): is_moving = Input.is_action_pressed("move_right") or Input.is_action_pressed("move_left") is_act = Input.is_action_pressed("kick") var animatedSprite = get_node("AnimatedSprite")

if (is_moving):
	player_speed = PLAYER_WALK_SPEED
	player_direction = Vector2()
	
	if(can_move and Input.is_action_pressed("move_right")):
		player_direction += RIGHT

	if(can_move and Input.is_action_pressed("move_left")):
		player_direction += LEFT
else:
	player_speed = 0
var movment = player_speed * player_direction.normalized() * delta
move_player(movment)
anim_player()

func move_player(movment): move(movment)

func anim_player(): var animatedSprite = get_node("AnimatedSprite")

if(is_act):
	if(player_direction == RIGHT):
		animatedSprite.play("kick_right")
	else:
		animatedSprite.play("kick_left")
elif(is_moving):
	if(player_direction == RIGHT):
		animatedSprite.play("walk_right")
	else:
		animatedSprite.play("walk_left")
else:
	idle_player()

func idle_player(): var animatedSprite = get_node("AnimatedSprite") if(player_direction == RIGHT): animatedSprite.play("stop_right") else: animatedSprite.play("stop_left")

func _on_AnimatedSprite_finished(): if(not is_moving): idle_player()

I made some edits to your code. I think the code below should work, in theory at least. I placed comments on the parts I changed.

    extends Node2D
    const PLAYER_WALK_SPEED = 180
    const PLAYER_RUN_SPEED = 200
    const RIGHT = Vector2(1, 0)
    const LEFT = Vector2(-1, 0)

    var player_speed = PLAYER_WALK_SPEED
    var player_direction = Vector2()
    var can_move = true
    var is_moving = false
    var is_act = false
    # We need a way to know when an animation is playing or not
    var is_animation_playing = false

    func _ready():
       set_fixed_process(true)

    func _fixed_process(delta):
       is_moving = Input.is_action_pressed("move_right") or Input.is_action_pressed("move_left")
       is_act = Input.is_action_pressed("kick")
       var animatedSprite = get_node("AnimatedSprite")

       # Update is_animation_playing
       is_animation_playing = animatedSprite.is_playing()

       if (is_moving):
          player_speed = PLAYER_WALK_SPEED
          player_direction = Vector2()

       # You had move_left on a separate if check and an else that sets speed to zero
       # but your right movement was in a separate if check. I switched the move_left if 
       # check to a elif check. (A minor nitpick, feel free to ignore! :) )
       if(can_move and Input.is_action_pressed("move_right")):
          player_direction = RIGHT
       elif(can_move and Input.is_action_pressed("move_left")):
          player_direction = LEFT
       else:
          player_speed = 0
       
       var movment = player_speed * player_direction.normalized() * delta
       move_player(movment)
       anim_player()

    func move_player(movment):
       move(movment)

    func anim_player():
       var animatedSprite = get_node("AnimatedSprite")

       # Are we playing a animation? If we are, then return so none of the code below is called.
       # NOTE: you could place this check before every animatedSprite.play if you wanted to.
       if (is_animation_playing == false):
          return;

       if(is_act):
          if(player_direction == RIGHT):
             animatedSprite.play("kick_right")
          else:
             animatedSprite.play("kick_left")
       elif(is_moving):
          if(player_direction == RIGHT):
             animatedSprite.play("walk_right")
          else:
             animatedSprite.play("walk_left")
       else:
             idle_player()

    func idle_player():
       var animatedSprite = get_node("AnimatedSprite")
       if(player_direction == RIGHT):
          animatedSprite.play("stop_right")
       else:
          animatedSprite.play("stop_left")

    # This function isn't really needed, since you're changing animations in the
    # anim_player function every _fixed_process call.
    # (Once again, a minor nitpick on my part. Feel free to ignore :) )
    func _on_AnimatedSprite_finished():
       if(not is_moving):
          idle_player()

Hopefully the changes work! :smile:

5 years later