AnimatedSprite2D is not playing

Godot Version

4.2.1

Question

Hello! I’m developing a 2d platformer, and I stuck at animations. You see, when the player pressed jump, my animation switches, and stops on the 1st frame. Can anyone explain me what is happening!?
My code:
extends CharacterBody2D

@export var acceleration_speed : int = 5000
@export var max_speed : int = 150
@export var jump_force : int = 250
@export var coyote_time : float = 0.5

var is_jumping : bool = false
var can_jump : bool = true

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

@onready var anim_sprite = $AnimatedSprite2D
@onready var coyote_timer = $Coyote_timer

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
if can_jump:
coyote_timer.start(coyote_time)
#get_tree().create_timer(coyote_time).timeout.connect(coyote_timeout)
else:
can_jump = true
coyote_timer.stop()

velocity.x = clamp(velocity.x, -max_speed, max_speed)

var direction = Input.get_axis("left", "right")

velocity.x = direction * max_speed

if direction != 0:
	velocity.x = move_toward(velocity.x, max_speed * direction, acceleration_speed * delta)
	if velocity.x < 0:
		anim_sprite.flip_h = true
	elif velocity.x > 0:
		anim_sprite.flip_h = false

if Input.is_action_just_pressed("jump") and can_jump:
	jump()
	is_jumping = true
	can_jump = false
if velocity.y > 0:
	is_jumping = false

#if Input.is_action_pressed("left"):
	#anim_sprite.flip_h = true
	#velocity.x -= acceleration_speed * delta
	#if is_on_floor() and !is_jumping:
		#if anim_sprite.animation != "run":
			#anim_sprite.play("run")
#if Input.is_action_pressed("right") and !is_jumping:
	#velocity.x += acceleration_speed * delta
	#anim_sprite.flip_h = false
	#if is_on_floor():
		#if anim_sprite.animation != "run":
			#anim_sprite.play("run")

update_animations(delta)

move_and_slide()

func jump() → void:
velocity.y = -jump_force
is_jumping = true

func update_animations(delta) → void:
if velocity.x != 0:
anim_sprite.play(“run”)
else:
anim_sprite.play(“idle”)

if velocity.y > 0 and anim_sprite.animation != "jump":
	anim_sprite.play("fall")
	is_jumping = false

elif is_jumping and anim_sprite.animation != "jump":
	anim_sprite.play("jump")

func coyote_timeout() → void:
can_jump = false

Didn’t seems to solve the issue, and is_jumping is designed for velocity, I’m using it in animations because i don’t know how to make jump animation. Everything seems to cause the same issue.

It seems like you’ve fragmented the jump into different functions to obtain specific information. I think it would be more interesting to reduce the number of lines and get this information in a simpler way.

I adjusted the jump issue so that it changes animations according to the character’s movement. See if this helps in any way:

var is_jumping := false

func _physics_process(delta):
    # Add the gravity.
    if not is_on_floor():
        velocity.y += gravity * delta
    
    if Input.is_action_just_pressed("jump") and is_on_floor():
        velocity.y = jump_force
        is_jumping = true
    elif is_on_floor():
        is_jumping = false
    
    jump()

func jump():
    if !is_on_floor():
        if velocity.y > 0:
            anim_sprite.play("jump")
        else:
            anim_sprite.play("fall")
    elif direction != 0:
        anim_sprite.play("run")
    else:
        anim_sprite.play("idle")