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

Get help using Construct 2

Post » 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
11
S
2
Posts: 262
Reputation: 1,309

Post » 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
89
S
30
G
95
Posts: 5,154
Reputation: 63,444

Post » 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
129
S
29
G
77
Posts: 5,188
Reputation: 54,281

Post » 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

Post » 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
187
S
103
G
7
Posts: 131
Reputation: 18,608

Post » 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

Post » 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
11
S
2
Posts: 262
Reputation: 1,309

Post » 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

Post » 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
11
S
2
Posts: 262
Reputation: 1,309

Post » 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

Return to How do I....?

Who is online

Users browsing this forum: drdisk, newt, SebastianOs, stevecameron and 10 guests