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

0 favourites
  • 4 posts
From the Asset Store
This is a Dungeon Master tool & the 1st of 12 Combat Engines from the Building Combat Engines for Browser Games workshop
  • 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

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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))

  • Thanks!

    I'll try those solutions.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)