How do I select a random sprite?

Get help using Construct 2

Post » Mon Mar 20, 2017 9:21 am

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
B
10
S
1
Posts: 45
Reputation: 804

Post » Mon Mar 20, 2017 9:34 am

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:
Code: Select all
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)

and then the rest of your events afterwards to control things like the message popup and restart the game.

hope that helps.
B
9
S
2
Posts: 69
Reputation: 833

Post » Mon Mar 20, 2017 9:37 am

Thanks MattTonkinson is this! Because I did not know how to do it.

Thanks very much
B
10
S
1
Posts: 45
Reputation: 804

Post » Mon Mar 20, 2017 9:42 am

No problem at all :)
B
9
S
2
Posts: 69
Reputation: 833

Post » Mon Mar 20, 2017 10:59 am

Something I'm doing wrong... @MattTonkinson can help me?? thanks :?

Image

Image
B
10
S
1
Posts: 45
Reputation: 804

Post » Mon Mar 20, 2017 11:07 am

Or can someone help me? Thank you.
B
10
S
1
Posts: 45
Reputation: 804

Post » Mon Mar 20, 2017 12:06 pm

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 :)

Image
B
9
S
2
Posts: 69
Reputation: 833

Post » Mon Mar 20, 2017 2:59 pm

Oh thanks again @MattTonkinson :idea:

Now the selector works perfect. :)
B
10
S
1
Posts: 45
Reputation: 804

Post » Mon Mar 20, 2017 3:02 pm

What if I did not want the selector to move randomly?
Should I then change the Random Round 0,3 code line?
B
10
S
1
Posts: 45
Reputation: 804

Post » Mon Mar 20, 2017 4:51 pm

senecaa wrote: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
Code: Select all
Set XXXX to round(random(0, 3))
to
Code: Select all
Set XXXX to XXXX % 3 + 1


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:
  • XXXX = 0, XXXX / 3 = 0, 0 remains => XXXX % 3 = 0
  • XXXX = 1, XXXX / 3 = 0, 1 remains => XXXX % 3 = 1
  • XXXX = 2, XXXX / 3 = 0, 2 remains => XXXX % 3 = 2
  • XXXX = 3, XXXX / 3 = 1, 0 remains => XXXX % 3 = 0
  • XXXX = 4, XXXX / 3 = 1, 1 remains => XXXX % 3 = 1
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
B
32
S
7
G
4
Posts: 398
Reputation: 4,591

Next

Return to How do I....?

Who is online

Users browsing this forum: blue visionary, fens, rayolf and 22 guests