How do I set a sprite's animation to a random frame?

Before proceeding, please use the forum search feature at the top of the page to check if your question has already been answered.

How do I…

How do I set a sprite’s animation to a random frame?

What is the expected result

When I run the game, the frame of the sprite’s animation should be at a random value, based on the number of frames in the animation itself. I want to add more frames at a later point in time, so I do not want to use fixed values.

What is the actual result

Animation always plays through to the last frame and stops at the last frame.

Related screenshots

Store the number of frames of your animation in a variable, then randomize in range (0, the variable you store them) and store it to another variable.

I believe the first frame is really zero. So, you would use random (count-1). Also, I believe that you need to pause the animation. I don’t know if you can just pause it at the beginning or you have to pause it after each animation change.

A variable isn’t needed unless you need the number elsewhere or it helps you better understand the process. I usually breakdown complicated formulas myself instead of creating lengthy single events.

I did Keith’s method and it works really well. thank you!

1 Like