Spawn objects without overlapping

J

Jebmv

Guest
Hello, I am trying to create random objects across the top of the game. I got that to work except I cant get them to come down without overlapping. How would I stop this from happening. Here is the piece of code I am using. I put this inside and empty object. And yes this is my first try at making a game.

num = irandom_range(1, 100)


if num < 5 {

xp = irandom_range(75, 544)
yp = 1
r = irandom_range(1,5)

if r>=1 and r<=4
makeThis = obj_bomb
else if r = 5
makeThis = obj_coin

abc = instance_create(xp, yp, makeThis);
abc.speed = 7;
abc.direction = 270;
}

Thank You
 

Yal

šŸ§ *penguin noises*
GMC Elder
Check whether the point is free of collisions, and keep making new random points until it isn't.
Code:
//generate xp and yp
while(place_meeting(xp,yp,obj_bomb) || place_meeting(xp,yp,obj_coin)){
  //generate xp and yp again
}
//actually spawn stuff
Note that the object running the code needs a sprite the same size as the bomb/coin so it can actually check for overlaps, though.
 
L

Latch

Guest
You could (If I understand the question) use place_meeting, this function "Checks for a collision between two instances at a given position" so you it would be used it like:

Code:
if !place_meeting (xp,yp,makeThis)
{
abc = instance_create(xp, yp, makeThis);
abc.speed = 7;
abc.direction = 270;
}

But the above code is an example and would need to be tailored to your needs better. (The ! infront of place_meeting means 'not' so its saying if no place_meeting)

and Ninja's, grrr, ha
 
J

Jebmv

Guest
Thank you that made it work. I have to make new sprites the same size. The ones I used were different sizes and when I changed one to match the other. Dont look so hot. Thank you for your help.
 
Top