0 votes

Hey, I've been trying to make my AnimatedSprite (root node Area2D) move in 4 directions. Most of my code is from the video tutorial for How to Make Your First 2D Game in Godot Docs. I noticed that the tutorial only allowed for two animations to be played, so i tried to add another one (since i can't flip my up and down animation, because they look different). here is the code of the animated sprite movement:

if direction.x != 0:
    $AnimatedSprite.animation = "left_right"
    $AnimatedSprite.flip_v = false 
    $AnimatedSprite.flip_h = direction.x < 0
elif direction.y > 0:
    $AnimatedSprite.animation = "up"
elif direction.y < 0:
    $AnimatedSprite.animation = "down"

I think the main problem is just that i'm not sure how to assign x and y values to different directions, so i tried using greater then and less then. When i play the scene, my sprite doesn't move. When i do it with two animations, it does but i don't know how to use more then one. If you have any help or know what i'm doing wrong/how to solve this it would really help. Thanks!!

side note: I don't normally post in forums so if there's any other information you need to help feel free to ask.

edit: just realised i was using the wrong keys. please excuse my stupidity =)

in Engine by (16 points)

1 Answer

0 votes

This should do the trick


This will play the animation repeatedly very fast so maybe try out something like this

if $AnimatedSprite.current_animation != "up":

Or just play the animation when the player makes a button press.

if Input.is_action_just_pressed("ui_up"):

Note that you have to use is is_action_just_pressed because if you use is_action_pressed it will play the animation repeatedly very fast.

by (22 points)
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.