0 votes

Hello!

I've been slowing teaching myself how to use Godot and have run into an issue animating my sprite

func _process (delta):
     if Input.is_action_pressed("move_left"):
          $AnimatedSprite.play("Move")
          $AnimatedSprite.flip_h = true
     if Input.is_action_pressed("move_right"):
          $AnimatedSprite.play("Move")
          $AnimatedSprite.flip_h = false
     else:
          $AnimatedSprite.play("Idle")

This is the code that I'm using to animate my character, without the "else", it'll act as intending, flipping the sprite and playing the walking animation, but the animation will just continuously play.

With the "else" statement at the end, only the right facing walking animation will play.

If I use the "else" statement on each "if Input.AnimatedSprite.Play()" line, the walking animation won't play at all, just the Idle when the character isn't moving

Any help would be appreciated, thank you!

in Engine by (15 points)

1 Answer

+2 votes
Best answer

Yeah, the logic you have there is problematic and likely not what you intend...

You're probably looking for this:

func _process (delta):
     if Input.is_action_pressed("move_left"):
          $AnimatedSprite.play("Move")
          $AnimatedSprite.flip_h = true
     elif Input.is_action_pressed("move_right"):
          $AnimatedSprite.play("Move")
          $AnimatedSprite.flip_h = false
     else:
          $AnimatedSprite.play("Idle")

That is basically "If left pressed, do the left animation. If right is pressed, do the right animation. If neither left or right are pressed, do the idle animation.

by (10,868 points)
selected by

I see, that did the trick, thank you for the help!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.