Giving an object a random subimage?

So what I've got, is a sprite called ball with 5 subimages, and an event placed in a room.

The events sprite is set at ball, and obviously if I test play, it just sits there spasming out switching through the 5 subimages...

What I'm trying to do is make it so the event will use one of the subimages as the sprite, but I want it to select it at random, so for example, I could close and open the game a few times, and it would (Hopefully, based on chance lol...) show a different sprite subimage each time.

Does anyone have any ideas as to how I could do this? It'd be a great help and really add some variety to my levels.

Thanks, and if you need anymore info, feel free to ask.
What you might want to do is just have 5 different sprites. Then, when variable [blank] is equal to zero, the sprite changes into [sprite blank] and so on. The only issue is that I do not know how you would set a random variable. :c

I hope this is what you are asking, Im not entirely sure tbh.

you can also change the sub-image with that option as well, but I do not know if it would keep spazzing out or not.
Lol me neither, but for the time being I've got a temporary solution, although I can only use 4 sprites... I've used the Create Random action, and made 4 objects with the same parent, and different sprites.

I mean sure, it may not be convenient, but it works for now, lol...
try in "create event" for the ball with "execute code" action

  1. image_speed=0 // set off the animation for the sprite
  2. image_index=floor(random(5))
Expand to see the code.


and hope it's work for you 🙂
You can simply do the following:

OBJECT- CREATE: Change sprite: Speed: 0 Image: random(5)

The five digit stands for your number of images in the sprite, INCLUDING the subimage 0 (zero). So you put your number of sumimages after the random text in the subimage box (loke int the exampel above!)

Another Example: I have 7 subimages and want it to randomize subimage: Change Sprite: Image: random(7).

Easy as that:)

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=71192