Spawning objects on random coordinates without overlaping.

Hello.
I am having a problem spawning objects on random coordinates.
The objects are spawning alright, but they overlap when (xpoint,ypoint) get the same value as before and i don’t know how to fix it.

[lua]local xTable = {50,108,158,214,266,57,108,160,211,264,54,108,162,214,267,53,107,162,215,267}

local yTable = {288,287,287,288,288,341,341,342,342,342,392,392,395,395,396,439,440,443,443,442}
local function spawnMole()

local xpoint = xTable[math.random(#xTable)]
local ypoint = yTable[math.random(#yTable)]

local mole = display.newImageRect(“mole-5-coloring-page[1].jpg”,49, 45);
mole.x = xpoint;
mole.y = ypoint;

end
tmr = timer.performWithDelay(500, spawnMole, 999);[/lua]

Help would be much appreciated. [import]uid: 30816 topic_id: 29058 reply_id: 329058[/import]

Well it should be much easier to solve since you have X/Y tables. Basically you need to either (A) loop to continue choosing random coordinates until you find unused ones, or (B) remove the coordinates that are being used from the table (after you set the x/y, of course)

(A) might give you a bit more flexibility but you’d need to write a function to compare and make sure they are unused first. (B) lets you just use table.remove(table, index) to rip out the used numbers and move on without doing a check. [import]uid: 41884 topic_id: 29058 reply_id: 116936[/import]

I want to spawn the objects on those coordinates, randomly, without overlaping and repeatedly for 60 seconds so i can’t remove any number from the tables. [import]uid: 30816 topic_id: 29058 reply_id: 117087[/import]

Solved it like this:

[lua]local xTable = {50,108,160,214,266,50,108,160,214,266,50,108,160,214,266,50,108,160,214,266}
local yTable ={288,288,288,288,288,341,341,341,341,341,392,392,392,392,392,440,440,440,440,440}
mRand = math.random;

local function spawnMole()

c = prevx
v = prevy

xpoint = xTable[mRand(#xTable)]
ypoint = yTable[mRand(#yTable)]

prevx = xpoint
prevy = ypoint
if c == xpoint or v == ypoint then
samecoord = true;
else
samecoord = false;
end

if (samecoord == false) then

local mole = display.newImageRect(“mole-5-coloring-page[1].png”, 49, 45);
mole.x = xpoint
mole.y = ypoint
else

repeat spawnMole() until (samecoord == false)

end
end

tmr = timer.performWithDelay(500, spawnMole, 999);[/lua] [import]uid: 30816 topic_id: 29058 reply_id: 117247[/import]

Not sure if you ever revisited this since 2012 but was wondering If this did indeed work? Its very good but I still get overlap :frowning:

What do these lines of code do and where did the variables come from? 

c = prevxv = prev

prevx = xpointprevy = ypointif c == xpoint or v == ypoint then

How do you check if the x&y position is in use, then after it is not being used the object will spawn in that position or spawn in a different x&y position that is not in use.

I think you’re going to have to provide more code and more context about what’s going on before any one can answer it.

It also looks like either you typed it in with an error or there was a copy/paste error.  I suspect this line:

prevx = xpointprevy = ypointif c == xpoint or v == ypoint then

should be

prevx = xpointprevy = ypoint

if c == xpoint or v == ypoint then

Not sure if you ever revisited this since 2012 but was wondering If this did indeed work? Its very good but I still get overlap :frowning:

What do these lines of code do and where did the variables come from? 

c = prevxv = prev

prevx = xpointprevy = ypointif c == xpoint or v == ypoint then

How do you check if the x&y position is in use, then after it is not being used the object will spawn in that position or spawn in a different x&y position that is not in use.

I think you’re going to have to provide more code and more context about what’s going on before any one can answer it.

It also looks like either you typed it in with an error or there was a copy/paste error.  I suspect this line:

prevx = xpointprevy = ypointif c == xpoint or v == ypoint then

should be

prevx = xpointprevy = ypoint

if c == xpoint or v == ypoint then