How to randomize sprite frames in Animated Sprite

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Trikshot48

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.

:bust_in_silhouette: Reply From: jgodfrey

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:


Thank you. That works.

$AnimatedSprite.frame = randi() % $AnimatedSprite.frames.get_frame_count("default")

DavidPeterWorks | 2022-08-13 20:34