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

## Recommended Posts

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?

##### Share on other sites

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

##### Share on other sites

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

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.