Animation won't play when changing it. It just stays at the first frame

Godot Version

4.1.1

Question

I am using sprite_2d.animation = “animation” (it is an animated sprite), but it only plays the first frame. I do have one animation that only plays one frame, but that shouldn’t be the issue.

Could you post a screenshot of the sprite frames panel? And/or more of your script?


Script:

extends CharacterBody2D
@onready var sprite_2d = $Sprite2D


const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var isLeft = false
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")


func _physics_process(delta):
	if (velocity.x > 1 || velocity.x < -1):
		sprite_2d.animation = "running"
	else:
		sprite_2d.animation = "idle"
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta
		sprite_2d.animation = "jumping"
	# Handle Jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("ui_left", "ui_right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()
	
	if velocity.x != 0: 
		isLeft = velocity.x < 0
	sprite_2d.flip_h = isLeft

I used the beginner tutorial from Coco Code.


Sorry it only lets me send one at a time.

Try changing to sprite_2d.play("animation") maybe the one-frame animation thinks it’s done animating?

I have tried that. It just crashes.

crashes? no error or stack trace?

Oh clearly I did sum wrong cuz it works just fine now! Thank you so much tho!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.