How do I select a random sprite?

0 favourites
  • 11 posts
From the Asset Store
A set of retro 16-Bit Neon UI elements to make your menus pop!
  • Hi,

    I explain: I have 3 sprites color box:

    1 red 2 blue 3 yellow

    And 1 sprite selector, its a simple bar (for example): _

    The simple bar must move randomly between the three boxes of colors until you press the space key .You must choose between three objects. When you press the space key the sprite selector (simple bar) will stop in one of the color boxes and I get a message.

    Is this possible in Construct2? I would like to make a random selector.

    I m sorry, I don't know if I explained well.

    Thanks a lot

  • Hi senecaa

    to pick a random number you can use the code "round(random(0,3))"

    This will pick a random float between 0 and 3 and round it up to an integer (whole number).

    To round it down instead use "floor(random(0,3))". This will floor the number, essentially rounding it down.

    So, to make this work with your application, you could do something like:

    Global Variable XXXX
    Event - every 0.25 seconds & XXXX /= to 0 -> Set variable XXXX to "Round(Random(0,3))"     (This will set the variable 4 times per second)
    event - if XXXX == 1  -> SLIDER.SetPosition to (whatever your X/Y position 1 needs to be)
    event - if XXXX == 2  -> SLIDER.SetPosition to (whatever your X/Y position 2 needs to be)
    event - if XXXX == 3  -> SLIDER.SetPosition to (whatever your X/Y position 3 needs to be)
    
    event - Keypress SPACE -> Set Variable XXXX to 0 (this will stop the slider moving)
    [/code:3nnjazec]
    and then the rest of your events afterwards to control things like the message popup and restart the game.
    
    hope that helps.
  • Thanks MattTonkinson is this! Because I did not know how to do it.

    Thanks very much

  • No problem at all

  • Something I'm doing wrong... can help me?? thanks

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Or can someone help me? Thank you.

  • The System->XXXX = 1 events need to all be separate events.

    you have all 3 in one event so it is checking "is it 1, is it 2, is it 3" all at the same time

  • Oh thanks again

    Now the selector works perfect.

  • What if I did not want the selector to move randomly?

    Should I then change the Random Round 0,3 code line?

  • What if I did not want the selector to move randomly?

    Should I then change the Random Round 0,3 code line?

    In that case, you should change

    Set XXXX to round(random(0, 3))[/code:10jfkiqt] to [code:10jfkiqt]Set XXXX to XXXX % 3 + 1[/code:10jfkiqt]
    
    What is that "%" between "XXXX" and "3", you might ask ... That is modulo, that is the remainder of dividing. 
    In my example, it will divide XXXX by 3 and get what remains:
    [ul][li]XXXX = 0, XXXX / 3 = 0, [b]0[/b] remains => XXXX % 3 = [b]0[/b][/li]
    [li]XXXX = 1, XXXX / 3 = 0, [b]1[/b] remains => XXXX % 3 = [b]1[/b][/li]
    [li]XXXX = 2, XXXX / 3 = 0, [b]2[/b] remains => XXXX % 3 = [b]2[/b][/li]
    [li]XXXX = 3, XXXX / 3 = 1, [b]0[/b] remains => XXXX % 3 = [b]0[/b][/li]
    [li]XXXX = 4, XXXX / 3 = 1, [b]1[/b] remains => XXXX % 3 = [b]1[/b][/li][/ul]
    and so on.
    
    Then, I add + 1 to it, so you get 1, 2, 3 instead of 0, 1, 2.
    
    The modulo operator is useful here to make sure your numbers stay between 1 and 3 instead of constantly going up and up to the moon. :p
  • Thanks a lot grigrizljac !

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