Here you go. This should work… Note that this will fail if no. of images you are trying to spawn is big.
[lua]local mapRightBoundary = display.contentWidth
local mapBottomBoundary = display.contentHeight
local phantomAircraft = {}
local abs = math.abs
local random = math.random
local function checkOverlap(obj1, obj2) --returns true if obj1 and obj2 are overlapping
local xdiff = abs(obj2.x - obj1.x)
local ydiff = abs(obj2.y - obj1.y)
local w1 = obj1.width
local w2 = obj2.width
local h1 = obj1.height
local h2 = obj2.height
if xdiff < (w1+w2)*0.5 and ydiff < (h1+h2)*0.5 then
return true
else
return false
end
end
local adjust
function adjust(obj, tbl) – adjusts position of obj such that it doesn’t overlap with any object in table tbl
for i = 1, #tbl do
if checkOverlap(obj,tbl[i]) then
obj.x = random (0, mapRightBoundary)
obj.y = random (0, mapBottomBoundary)
adjust(obj,tbl)
end
end
end
for i=1,10 do
local tempImg = display.newImage(“airplane.png”)
tempImg.isVisible = false
tempImg.x = random (0, mapRightBoundary)
tempImg.y = random (0, mapBottomBoundary)
adjust(tempImg,phantomAircraft)
phantomAircraft[i] = tempImg
phantomAircraft[i].isVisible = true
tempImg = nil
end [/lua] [import]uid: 48521 topic_id: 11270 reply_id: 40963[/import]