Something like this should do it, although I can’t test it as I’m at work - there’s usually a stupid mistake somewhere in new code!
You also might need to build in a limit for the number of balloons, or a limit on the number of attempts to find a position for the balloon, as if there’s nowhere to put it you’ll get stuck in the loop.
[lua]-- setup table to store balloon objects
local balloons = {}
----- SPAWN RANDOM BALLOONS {1} -----
– destroy balloon when touched
local function tapBalloon(event)
local obj = event.target
local phase = event.phase
if phase == “ended” then
obj:removeSelf()
– could put code to add points to a score here
end
end
local function spawnRandom()
local rand = math.random( 300 )
local j
local randx
local randy
local ballooncnt = table.maxn(balloons)
local ok = false
– loop through all balloons and check no overlaps, if so
– generate new position and test again
while (ok ==false) do
randx = math.random(35,285)
randy = math.random(0, 300)
for i = 1, ballooncnt,1 do
local obj = balloons[i]
local gapx = math.abs(obj.x - randx)
local gapy = math.abs(obj.y - randy)
if (gapx > obj.width or gapy > obj.height ) then
ok = true
end
end
end
– decide which image to use
if (rand < 100) then
j = display.newImage(“random1.png”)
elseif (rand < 200) then
j = display.newImage(“random2.png”)
else
j = display.newImage(“random3.png”)
end
– common code no matter which image selected
j:setReferencePoint(display.CenterReferencePoint)
j.x = randx
j.y = randy
j.myName = “j”
j:addEventListener(“touch”, tapBalloon)
balloons[ballooncnt+1] = j
RandomGroup:insert(j)
end
– Round Balloon Timer –
local Drop = timer.performWithDelay(1000, spawnRandom, 0) [/lua]
[import]uid: 93133 topic_id: 16321 reply_id: 63529[/import]