• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Making an object appear randomly (range/open spots)

S

Shajian28

Guest
I am creating a game in which fruit randomly appear on the screen (within a set range).

The problem is that the fruit sometimes appears in a wall. When the fruit hits a wall it jumps to a random spot again until it finds an open spot. The game works, but I know the coding could be more efficient.

Is there a way to make the fruit randomly appear in an open spot the first time every time?

This is the code I got so far (when fruit is initially created):
instance_create(random_range(32,704), random_range(96,416), obj_orange)
 

Slyddar

Member
The method you are using is fairly common. If you are worried, add a counter that shows how many times each spawn hits a wall, which would show the efficiency of the code. If it's only hitting a wall a few times before finding a blank spot, that's fine. If it's happening hundreds, well, then you might need to rethink the code.

If you had to have a workaround, which I really don't think you need as it's a bit cumbersome, but since you asked:
1/ Zone your open areas into rectangles and record the top left and bottom right of each zone. Then you can choose a zone at random, and then spawn an object into that space. The fruit will be created in open space each time.
 
S

Shajian28

Guest
The method you are using is fairly common. If you are worried, add a counter that shows how many times each spawn hits a wall, which would show the efficiency of the code. If it's only hitting a wall a few times before finding a blank spot, that's fine. If it's happening hundreds, well, then you might need to rethink the code.

If you had to have a workaround, which I really don't think you need as it's a bit cumbersome, but since you asked:
1/ Zone your open areas into rectangles and record the top left and bottom right of each zone. Then you can choose a zone at random, and then spawn an object into that space. The fruit will be created in open space each time.
That's a great idea, thanks so much!
 
Top