How do I make an AnimatedSprite move all 4 directions with $AnimatedSprite?

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

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 =)

:bust_in_silhouette: Reply From: RedCube

This should do the trick

$AnimatedSprite.play("up")

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

if $AnimatedSprite.current_animation != "up":
    $AnimatedSprite.play("up")

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

if Input.is_action_just_pressed("ui_up"):
    $AnimatedSprite.play("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.