AnimationSprite2d not playing

Godot Version

4.5.1

Question

Hi, i’m new to coding, my AnimatedSprite2d is not playing when i enter the game. It works when I hit autoplay in the node but the other animations are not playing when i’m moving in game. this is the script I used


extends CharacterBody2D

const SPEED = 100.0

@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D

func _physics_process(delta: float) → void:
process_movement()
move_and_slide()

func process_movement() → void:
var direction := Input.get_vector(“left”, “right”, “up”, “down”)
velocity = direction * SPEED

func play_animation(dir: Vector2):
if dir.x > 0:
animated_sprite_2d.play(“walk_left”)
elif dir.x < 0:
animated_sprite_2d.play(“walk_right”)
elif dir.y > 0:
animated_sprite_2d.play(“idle_down”)
elif dir.y < 0:
animated_sprite_2d.play(“idle_up”)

The engine will call _physics_process for you automatically. However, other functions you write yourself will not be called automatically. You have to tell the engine yourself when they should be called.

Currently, play_animation is a function you have defined. But the script itself doesn’t use it. Consider calling it inside process_movement like this:

func process_movement()->void:
    var direction := Input.get_vector(“left”, “right”, “up”, “down”)
    play_animation(direction)
    velocity = direction * SPEED