0 votes

I’m currently trying to make a 2D game with dice and I’m trying to figure out how to randomize the frames of the dice animation so that it picks a random side. So far I’ve only been able to figure out how to pick random animations, but not how to randomize the frames inside one animation.

in Engine by (12 points)

1 Answer

+1 vote

Looks like you can get (or set) the current frame number in an AnimatedSprite via its frame property. So, setting that to an appropriate value should shift the animation to the defined frame.

So, something like:

$AnimatedSprite.frame = 4

If you need the number of frames in the animation (to drive the random frame selection) you can get that via:

$AnimatedSprite.frames.get_frame_count('your_animation_name')

by (21,756 points)

Thank you. That works.

$AnimatedSprite.frame = randi() % $AnimatedSprite.frames.get_frame_count("default")
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.