0 votes


It's possible to advance the animation frame manually? I mean: supose that I have an animation with 4 frames, if I press the Enter button, the animation frame will increase in one step till I reach index 3 and, if I press enter again, the frame will return from beginning, in other words, to frame 0. I thought Godot had a function like AnimatedSprite.next_frame(), but not.

How could I implement this?

in Engine by (22 points)

1 Answer

+1 vote
Best answer

You could do this with set_frame().

You'd first need to use get_sprite_frames().get_frame_count() to know how many frames you have, and then keep track of which frame you're on, so you can reset back to 0 when you reach the end. The % operator is useful for this.

It would work something like this:

extends AnimatedSprite

var num_frames
var current_frame = 0

func _ready():
    num_frames = get_sprite_frames().get_frame_count("anim_name")

func _input(event):
    if input.is_action_pressed("ui_select"):
        current_frame = (current_frame + 1) % num_frames

AnimatedSprite reference:

by (21,930 points)
selected by

That's what I was thinking! But if my animation has 4 frames (from 0 to 3) what happens when the variable current_frame is higher than 3? The set_frame return to 0 ?

Yes, because (3 + 1) % 4 is 0

Owww I didn't notice the (current_frame + 1) % num_frames, my bad.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.