How do I set sprite instances to random but no two the same?

Get help using Construct 2

Post » Thu May 08, 2014 7:54 pm

Hi,

I have 4 instances of a sprite object with 12 frames.
On sart of layout, I set sprite to random(0,11).
But sometimes 2 or more of the sprites show the same frame.

How can I ensure that no two frames are the same?

Thank you
B
7
S
1
Posts: 32
Reputation: 514

Post » Thu May 08, 2014 8:35 pm

Hi

You have to safe the random number generated and if the next random number generated matches a previous one you have to again the random number generator.
Cyberphaze

What we build can never be destroyed
B
136
S
18
G
6
Posts: 37
Reputation: 14,744

Post » Thu May 08, 2014 8:41 pm

One idea is to use a text list and use and remove one item at a time.

global text frameList=",0,1,2,3,4,5,6,7,8,9,10,11,"
global number randFrame=0

Start of layout
For each sprite
--- set randFrame to tokenat(frameList, random(1, tokencount(frameList, ",")), ",")
--- set frameList to replace(frameList, ","&randFrame&",", ",")
--- sprite set animation frame to randFrame


Another idea that equalizes eventually is:

repeat 12 times
Sprite animation frame is loopindex
system compare sprite.pickedcount > 1
--- sprite set animation frame to int(random(12))

Or if you want to make it work in one tick you can do this:

global number unique = 1

while
unique=1
--- set unique to 0
------ repeat 12 times
------- Sprite animation frame is loopindex
------- system compare sprite.pickedcount > 1
---------- sprite set animation frame to int(random(12))
B
91
S
31
G
103
Posts: 5,235
Reputation: 67,756

Post » Fri May 09, 2014 9:48 am

Thanks!

I'll try those solutions.
B
7
S
1
Posts: 32
Reputation: 514


Return to How do I....?

Who is online

Users browsing this forum: marcoscodas and 13 guests