1

Here is the code;

func _input(event):
    if event.is_action_pressed("move_right"):
        $player_animator.play("walking_animation")
    if event.is_action_pressed("move_left"):
        $player_animator.play("walking_animation")
    if event.is_action_pressed("move_up"):
        $player_animator.play("walking_animation")
    if event.is_action_pressed("move_down"):
        $player_animator.play("walking_animation")

I tried using .stop to $player_animator (the animation player). Then I tried making a variable "is running" and making that variable false normally and true while I pressed the movement keys. But they all failed to do anything.

Birko
  • 11
  • 1

1 Answers1

1

Calling stop() on the AnimationPlayer should stop the animation, but the problem might be: when/where are you calling it?

You are currently using func _input(event), which is event-based input handling. A single event is sent when a key is pressed, and another when the key is released. You could handle the is_action_released as well, and call AnimationPlayer.stop() there. But you would need to do this for all used keys.

The easier solution is to switch to polling-based input handling. Considering your player is a CharacterBody, you can implement func _physics_process(delta):

func _physics_process(delta):
if Input.is_action_pressed("move_right"):
    $player_animator.play("walking_animation")
elif Input.is_action_pressed("move_left"):
    $player_animator.play("walking_animation")
elif Input.is_action_pressed("move_up"):
    $player_animator.play("walking_animation")
elif Input.is_action_pressed("move_down"):
    $player_animator.play("walking_animation")
else:
    $player_animator.stop()

The CharacterBody2D tutorial lists several ways of handling input with polling. I advise using Input.get_axis() as it handles the case of both keys being pressed, and joysticks.

Bgie
  • 523
  • 3
  • 10