Animation sprites not working

Godot Version

4.7

Question

Hello, Godot community!
I have ran into a problem when making an animated walk cycle. The sprites don’t change and the animation isn’t playing.
It was actually working fine at first but then I added the ability to walk up and down and something at some point went wrong.

Here’s the code for the anim movement:

func _physics_process(_delta: float) → void:

if (velocity.x > 1):
	sprite_2d.play("right_walk")
elif (velocity.x < -1):
	sprite_2d.play("left_walk")
else:
	sprite_2d.play("default")
	
if (velocity.y < -1):
	sprite_2d.play("back_walk")
elif (velocity.y > 1):
	sprite_2d.play("front_walk")
else:
	sprite_2d.play("default")

I’m aware that it’s probably one (or both) of the following problems:

  1. It’s stuck repeating the first frame and the anim can’t finish
  2. One of the anims is overriding all others
    The catch is that I don’t know how to solve these issues. If anyone has any ideas, please explain potential fixes (*preferrably in simple terms).
    *I’ve been using Godot for only 3 days now and don’t have much programming experience overall.

Thank you in advance :]

you have two blocks of if/elif/else, both blocks will run one of the specified play functions.

If velocity is zero on both axis it will run play("default") twice, which luckily continues playing the animation.
If velocity.x is non-zero it will play a left or right walk animation, then it will interrupt that animation with the next block of if/elif/else.
If velocity.y is non-zero it will play a back/front walk animation, but be interrupted next frame by that previous block.

Thank you so much for pointing this out!!
I put everything into one if statement and it works perfectly now! You made my day so much better :]