Godot is telling me I need to add a ':' at the end of the line when I already have one and I can't figure out the issue...

else:
		if velocity == Vector2(0,0) and facing direction == "right":
			$AnimatedSprite.play("right_idle")
		elif facing_direction == left:
			$AnimatedSprite.play("left_idle")
		elif facing_direction == "up":
			$AnimatedSprite.play("up_idle")
		elif facing_direction == "down":
			$AnimatedSprite.play("down_idle")

Can anyone help?

The first line looks like "facing direction" is missing an underscore.

Also, checking "velocity == Vector2(0,0)" is probably not a good idea and most likely won't work.

You can try this:

if velocity.length_squared() < 0.01:
    # velocity is close to zero

Thanks, no errors now :)

Unfortunately, however, it isn't working in game. My sprite is stuck in the walking animation.

Here's the full script if it helps:

extends KinematicBody2D

export (int) var speed = 10

const MAX_SPEED = 75

var x = position.x
var y = position.y
var oldx = position.x
var oldy = position.y

var char_direction = Vector2()
var facing_direction = "0"

var is_diagonal = false

var velocity = Vector2.ZERO
var last_direction = Vector2(0, 1)

var anim_player = null
var currentAnim = ""
var newAnim = ""
var anim = ""

var playing = false

var state_machine

func _ready():
	anim = get_node("AnimationPlayer")
	set_process(true)
	
func _physics_process(delta):
	var input_vector = Vector2.ZERO
	input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
	input_vector = input_vector.normalized()
	
	if input_vector != Vector2.ZERO:
		velocity = input_vector * MAX_SPEED
	else:
		velocity = Vector2.ZERO
		
	move_and_collide(velocity * delta)
	
	oldx = position.x
	oldy = position.y
	velocity = move_and_slide(velocity)
	
	if char_direction.abs().x > 0 and char_direction.abs().y > 0:
		is_diagonal = true
	else:
		is_diagonal = false
		
	if is_diagonal == true:
			global_position = global_position.round()
		
func _process(delta):

	if (Input.is_action_pressed("ui_left")):
			$AnimatedSprite.play("left_walk")
			var facing_direction = "left"
	
	elif (Input.is_action_pressed("ui_right")):
			$AnimatedSprite.play("right_walk")
			var facing_direction = "right"
			
	elif (Input.is_action_pressed("ui_up")):
			$AnimatedSprite.play("up_walk")
			var facing_direction = "up"
			
	elif (Input.is_action_pressed("ui_down")):
			$AnimatedSprite.play("down_walk")
			var facing_direction = "down"
	
	else:
		if velocity.length_squared() < 0.01 and facing_direction == "right":
				$AnimatedSprite.play("right_idle")
		elif facing_direction == "left":
				$AnimatedSprite.play("left_idle")
		elif facing_direction == "up":
				$AnimatedSprite.play("up_idle")
		elif facing_direction == "down":
				$AnimatedSprite.play("down_idle")

Oh, I figured it out. All those extra 'var''s were the issue. Thank you anyway :)

Yes, when you use var on an existing variable name it will make a new variable with the same name and shadow it (sort of like overriding it temporarily). This is almost never what you want, as you have discovered. Cool to hear you figured it out.

3 years later