I’m trying to make a game for a game jam and i’m figuring out the movement system. I also need to make the character do an animation when going in diagonal but the animation just freezes here’s a code snippet:
if Input.is_action_pressed("right"):
$AnimatedSprite2D.play("walk_right")
if Input.is_action_pressed("left"):
$AnimatedSprite2D.play("walk_left")
if Input.is_action_just_released("left"):
$AnimatedSprite2D.play("idle_left")
if Input.is_action_just_released("right"):
$AnimatedSprite2D.play("idle_right")
if Input.is_action_pressed("down"):
$AnimatedSprite2D.play("walk_down")
if Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_up")
if Input.is_action_just_released("down"):
$AnimatedSprite2D.play("idle_down")
if Input.is_action_just_released("up"):
$AnimatedSprite2D.play("idle_up")
if Input.is_action_pressed("right") and Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_right")
if Input.is_action_pressed("left") and Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_left")
if Input.is_action_pressed("right") and Input.is_action_pressed("down"):
$AnimatedSprite2D.play("walk_right")
if Input.is_action_pressed("left") and Input.is_action_pressed("down"):
$AnimatedSprite2D.play("walk_left")
It’s a mess but the problem is in the last ligns, hope you can help.`
I think that’s what it’s doing, what can i do then ?
The slightly altered code :
func _process(_delta):
#idle
if Input.is_action_just_released("right"):
$AnimatedSprite2D.play("idle_right")
if Input.is_action_just_released("down"):
$AnimatedSprite2D.play("idle_down")
if Input.is_action_just_released("up"):
$AnimatedSprite2D.play("idle_up")
if Input.is_action_just_released("left"):
$AnimatedSprite2D.play("idle_left")
#up and down
if Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_up")
if Input.is_action_pressed("down"):
$AnimatedSprite2D.play("walk_down")
#diagonals
if Input.is_action_pressed("right") && Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_right")
if Input.is_action_pressed("left") && Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_left")
if Input.is_action_pressed("right") && Input.is_action_pressed("down"):
$AnimatedSprite2D.play("walk_right")
if Input.is_action_pressed("left") && Input.is_action_pressed("down"):
$AnimatedSprite2D.play("walk_left")
It’ll will evaluate ALL that match. So if you press up and left, it won’t play diagonal, it’ll fight with all for the animation player.
Handle Diagonals first, then under them handle single directions.
func handle_movement_animation():
if Input.is_action_pressed("right") and Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_right_up")
elif Input.is_action_pressed("left") and Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_left_up")
# Keep going with diagonals.
# Then use single directions checks for Up, right..etc
Then keep going…
else:
$AnimatedSprite2D.animation.begins_with("walk_right"):
$AnimatedSprite2D.play("idle_right")
# handle the idle logic here yeah?
Start looking into StateMachine design, plenty of tutorials online.
It’s better to use that philosophy to handle player during “Fight”, “Walking”, and other states instead of trying to keep track of elif statements and their priorities in code.