# What's the best way to execute a random event?

2 replies to this topic

### #1 ioi_xd OFFLINE

ioi_xd

• 13 posts

Posted Sun Apr 22, 2018 8:08 PM

I'm trying to make it so then when I shoot the enemy object, it respawns at a random part of the map in a different color. I got the color thing set, but I have one problem: Due to the way the playfield is set up, I can only have the player come out from four specified areas of the map.

I have worked out some code using "rand", but it doesn't seem to be working.

``` d = 0

[...]

if collision(player0,missile1) then d = rand
if rand < 4 then d = 4

if d = 1 then player0x = 80 : player0y = 10
if d = 2 then player0x = 140 : player0y = 50
if d = 3 then player0x = 85 : player0y = 90
if d = 4 then player0x = 10 : player0y = 50```

The missile doesn't seem to even affect the enemy, it just goes right through it.

Any idea on what I can do?

### #2 Random Terrain ONLINE

Random Terrain

Visual batari Basic User

• 28,867 posts
• Controlled Randomness
Replay Value
Nonlinear
• Location:North Carolina (USA)

Posted Sun Apr 22, 2018 10:04 PM

You might want to do it like this instead:

```   if !collision(player0,missile1) then goto __Skip_Respawn

temp5 = rand

if temp5 < 65 then then player0x = 80 : player0y = 10
if temp5 > 64 && temp5 < 129 then player0x = 140 : player0y = 50
if temp5 > 128 && temp5 < 193 then player0x = 85 : player0y = 90
if temp5 > 192 then player0x = 10 : player0y = 50

__Skip_Respawn
```

### #3 bogax OFFLINE

bogax

Dragonstomper

• 787 posts

Posted Mon Apr 23, 2018 5:44 PM

or

```  ;  rand & 3 will be a random number 0..3

if collision(player0, missile1) then d = rand & 3 : player0x = P0xrnd[d] : player0y = P0yrnd[d]

data P0xrnd
80, 140,  85,  10
end

data P0yrnd
10,  50,  90,  50
end
```

Edited by bogax, Mon Apr 23, 2018 5:46 PM.

#### 0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users