West(left)-animation not working after adding intercardinal animations.
Essentially, I am following DevWorms tutorial on YouTube and got the character to move and to animate in the main cardinal directions. However, when I add the intercardinal directions (NE, SE, SW, NW), the East/left) animation stops working. All other animations work, though, and all movements work as well.
I would appreciate any and all help here.
extends CharacterBody2D
var speed = 100
var player_state
func _physics_process(delta):
var direction = Input.get_vector(“left”, “right”, “up”, “down”)
if direction.x == 0 and direction.y == 0:
player_state = "idle"
elif direction.x != 0 or direction.y != 0:
player_state = "walking"
velocity = direction * speed
move_and_slide()
play_anim(direction)
func play_anim(dir):
print(dir)
if player_state == “idle”:
$AnimatedSprite2D.play(“idle”)
if player_state == “walking”:
if dir.y == -1:
$AnimatedSprite2D.play(“n-walk”)
if dir.x == 1:
$AnimatedSprite2D.play(“e-walk”)
if dir.y == 1:
$AnimatedSprite2D.play(“s-walk”)
if dir.x == -1:
$AnimatedSprite2D.play(“w-walk”)
if dir.x > 0.5 and dir.y < -0.5:
$AnimatedSprite2D.play("ne-walk")
if dir.x > 0.5 and dir.y > 0.5:
$AnimatedSprite2D.play("se-walk")
if dir.x < -0.5 and dir.y > 0.5:
$AnimatedSprite2D.play("sw-walk")
if dir.x < -0.5 and dir.y < 0.5:
$AnimatedSprite2D.play("nw-walk")
I need more information.
I see in your code that you print out whatever dir-parameter is passed to play_anim(), did you add a breakpoint or print whenever you trigger one of the intercardinal directions? When you wrote that
the animation stops working
does it mean the instructions are ignored? Or your character completely stops moving? Or is there an error saying Animation does not exist: "ne-walk" ?
I added the print statement just to see if there were some shenanigans going on with the movement speed, but it was pretty much within range, i.e: 0, 0.7, 1, and the negative values respectively.
Also, sorry for the (surely) stupid question, but what is a breakpoint?
“does it mean the instructions are ignored? Or your character completely stops moving? Or is there an error saying Animation does not exist: “ne-walk” ?”
Basically everything works as it should except the east/left animation when I am commanding the character to move to east/left. The animations in all other 7 directions work, and the character moves in all 8 directions. If I remove the code for the intercardinal animations, then I lose the animations for the intercardinal directions, as is expected, but it also animates the east/left movement. I.e. removing the code for the intercardinal animation fixes the main problem.
Also, sorry for the (surely) stupid question, but what is a breakpoint?
I added an image to explain the concept of breakpoints.
In short, it stops your running game at that point and gives you insight on all variables, their values and how the code proceeds
You can use that to go one-by-one through each line and see which animation is being actually played.
Right now I assume your character does correctly move west/left but the program continues and somehow enters another if-statement and “overwrites” the correct play() statement with something wrong.