# How do you make a random event?( capx needed )

Get help using Construct 2

### » Thu Jan 07, 2016 8:47 pm

How do you make an action based on random? I know about the random expression but i don't know how to use it. I don't want you to explain it. You should make a capx whit an enemy that can do four things. Shoot up, shoot to the rigth and shoot to the left and shoot down. The enemy should have a 25 % of doing onw of those four things every four seconds. The enemy should be completly coulored green and nothing else and the shots the same. Then share it whit a link in some way. I have asked this question before but now i ask it whit more clearity.
B
12
S
2
Posts: 267
Reputation: 1,344

### » Thu Jan 07, 2016 10:07 pm

every 4.0 seconds
for each enemy
--- create bullet at (enemy.x, enemy.y)
--- bullet: set angle to 90*int(random(4))
B
98
S
36
G
131
Posts: 5,521
Reputation: 83,505

### » Thu Jan 07, 2016 10:25 pm

Calfuso wrote:How do you make an action based on random? I know about the random expression but i don't know how to use it. I don't want you to explain it. You should make a capx whit an enemy that can do four things. Shoot up, shoot to the rigth and shoot to the left and shoot down. The enemy should have a 25 % of doing onw of those four things every four seconds. The enemy should be completly coulored green and nothing else and the shots the same. Then share it whit a link in some way. I have asked this question before but now i ask it whit more clearity.

Demanding someone creates a .capx with exactly what you want is not the right way of asking for assistance.
If your vision so exceeds your ability, then look to something closer.
Moderator
B
142
S
32
G
89
Posts: 5,812
Reputation: 62,218

### » Thu Jan 07, 2016 10:30 pm

While R0J0's answer is correct, I thought I'd take a moment to explain why this works.

random(x) returns a random number between 0 and x, Including random decimal points. So therefore random(4) could return 0, or 1.75 or 3.14 or 1.1217597243 or whatever, as long as it's a positive number and below (but not including) 4.

This is where the int() comes in. By using the int() expression he converts the number to one that does not have fractions, it's effectively the same as using floor(). 1.75 would become 1, 3.14 would become 3, and there are now only 4 possible numbers that int(random(4)) can produce: 0, 1, 2 or 3 (each with an equal chance).

Now by taking this and multiplying it by 90, your possible angles for the bullet are 0, 90, 180 and 270, which does exactly as you wanted it to.
B
11
S
3
G
1
Posts: 98
Reputation: 1,062

### » Fri Jan 08, 2016 6:35 am

Robsta wrote:While R0J0's answer is correct, I thought I'd take a moment to explain why this works.

random(x) returns a random number between 0 and x, Including random decimal points. So therefore random(4) could return 0, or 1.75 or 3.14 or 1.1217597243 or whatever, as long as it's a positive number and below (but not including) 4.

This is where the int() comes in. By using the int() expression he converts the number to one that does not have fractions, it's effectively the same as using floor(). 1.75 would become 1, 3.14 would become 3, and there are now only 4 possible numbers that int(random(4)) can produce: 0, 1, 2 or 3 (each with an equal chance).

Now by taking this and multiplying it by 90, your possible angles for the bullet are 0, 90, 180 and 270, which does exactly as you wanted it to.

What's the difference between using floor() and int()?
B
246
S
136
G
12
Posts: 167
Reputation: 25,965

### » Fri Jan 08, 2016 7:58 am

floor takes a number and rounds it down to the nearest whole number.

int converts a variable/number into an integer (which by definition, does not have decimal places, aka a whole number).

So basically floor(3.14) gives a real number which is 3.00
While int(3.14) gives an integer which is 3
B
11
S
3
G
1
Posts: 98
Reputation: 1,062

### » Fri Jan 08, 2016 5:53 pm

R0J0hound wrote:every 4.0 seconds
for each enemy
--- create bullet at (enemy.x, enemy.y)
--- bullet: set angle to 90*int(random(4))

That information was helpful and now i know how to make a enemy shoot in random directions. But i still want to know how to make enemys spawn in random image points of a sprite and how to make enemy switch to random animations. I tried usiing the same solution as for shooting in random directions but it does not work.
B
12
S
2
Posts: 267
Reputation: 1,344

### » Fri Jan 08, 2016 6:10 pm

You could also use choose instead of random:

For animations

> sprite set animation to : choose("animation1","animation2") etcetera

For spawning at imagepoints

> sprite spawn object on layer at imagepoint choose(0,1,2,3,4,5)
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

### » Sat Jan 09, 2016 7:57 pm

LittleStain wrote:You could also use choose instead of random:

For animations

> sprite set animation to : choose("animation1","animation2") etcetera

For spawning at imagepoints

> sprite spawn object on layer at imagepoint choose(0,1,2,3,4,5)

When i try to make a sprite switch to random animations i write in the words exactly like you did and i keep getting the error message "expression seem to end before here. are you missing something before it. Other then that the information was very helpful.
B
12
S
2
Posts: 267
Reputation: 1,344

### » Sat Jan 09, 2016 8:32 pm

Calfuso wrote:
LittleStain wrote:You could also use choose instead of random:

For animations

> sprite set animation to : choose("animation1","animation2") etcetera

For spawning at imagepoints

> sprite spawn object on layer at imagepoint choose(0,1,2,3,4,5)

When i try to make a sprite switch to random animations i write in the words exactly like you did and i keep getting the error message "expression seem to end before here. are you missing something before it. Other then that the information was very helpful.

You didn't write etcetera, did you?
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Next