Animation timer problem

Godot Version

`Godot 4.3

Question

`Hello I hope you are well I need help I am working on a fan game for Sonic the Hedgehog the game 2D So now I’m working on the mechanics and physics of Sonic’s character and also acceleration and deceleration. I have a problem in Animated Sprite 2D when I add a timer to change the animation from run to run_fast, first the player position doesn’t change Also, animation does not work.

This is the script, I hope you can help me.

extends CharacterBody2D

const SPEED = 800.0 # Maximum speed
const ACCELERATION = 800.0 # Acceleration when moving
const DECELERATION = 6000.0 # Deceleration when stopping
const JUMP_VELOCITY = -700.0 # Jump velocity
const GRAVITY = 1500.0 # Gravity (adjust based on your project settings)

Animation variables

@onready var animated_sprite = $AnimatedSprite2D # Access AnimatedSprite2D
@onready var timer = $Timer # Access Timer in the scene

var is_moving = false # To check if the player is moving (left or right)
var has_played_fast_animation = false # To track if the fast animation has been played

Timer setup

func _ready():
timer.autostart = false # Ensure the timer doesn’t start automatically

func _physics_process(delta: float) → void:
# Apply gravity
if not is_on_floor():
velocity.y += GRAVITY * delta

# Handle jumping
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
    velocity.y = JUMP_VELOCITY

# Get movement direction and handle acceleration/deceleration
var direction := Input.get_axis("ui_left", "ui_right")

if direction != 0:
    # If the player is moving, play the animation
    if not is_moving:
        animated_sprite.play("run")  # Play the initial run animation
        timer.start(2.0)  # Start the timer for 2 seconds
        is_moving = true  # Set the variable to indicate the player started moving
        has_played_fast_animation = false  # Reset the fast animation state
else:
    # When stopping, show the default (Idle) animation
    animated_sprite.play("idle")
    is_moving = false  # Reset the moving state when stopping
    timer.stop()  # Stop the timer
    has_played_fast_animation = false  # Reset the fast animation state

# Handle acceleration/deceleration
if direction != 0:
    velocity.x = move_toward(velocity.x, direction * SPEED, ACCELERATION * delta)
else:
    velocity.x = move_toward(velocity.x, 0, DECELERATION * delta)

# Apply movement using move_and_slide
velocity = move_and_slide(velocity)

When the Timer finishes after 2 seconds, change the animation

func _on_Timer_timeout():
if is_moving and not has_played_fast_animation:
animated_sprite.play(“run_fast”) # Change to the fast run animation
has_played_fast_animation = true # Mark that the fast animation has been played