16 posts
• Page **1** of **2** • **1**, 2

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.

**Posts:**267**Reputation:**1,344

B

12
S

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

**Posts:**5,556**Reputation:**60,456

Moderator

B

137
S

31
G

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

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.

**Posts:**98**Reputation:**1,062

B

11
S

3
G

1
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()?

**Posts:**150**Reputation:**23,226

B

230
S

128
G

9
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

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

**Posts:**98**Reputation:**1,062

B

11
S

3
G

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

**Posts:**267**Reputation:**1,344

B

12
S

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

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

**Posts:**6,160**Reputation:**19,775

B

54
S

16
G

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

**Posts:**267**Reputation:**1,344

B

12
S

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

**Posts:**6,160**Reputation:**19,775

B

54
S

16
G

8
16 posts
• Page **1** of **2** • **1**, 2

## Who is online |

Users browsing this forum: Yahoo [Bot] and 29 guests |