Spawn Image On Random Positions

Thanks in advance,

I been struggling this past days trying to create Random Images spawn
on Random Positions on screen but the trick is I do not want the image
to spawn on top of each other or close enough to bump to each other.

So far I have everything working properly:

  • Random Images are spawning
  • Spawning on different positions

But the problem is sometimes they spawn very close or nearly on top
of each other and causes them to bump one another and makes the
my game look unreal :frowning:

Note: even if you use math.randomseed( os.time() ) and call your
x, y using math.random eventually they will spawn close or nearly
on top of each other. [import]uid: 30314 topic_id: 16321 reply_id: 316321[/import]

When you spawn an image ( before it is actually shown, just when you get its position ) you could check its position against a list of all previously spawned images ( or all active images if they get removed over time ) and then move/get a new position if needed. [import]uid: 5833 topic_id: 16321 reply_id: 60781[/import]

as Graham said…

you can also have a loop as many pro games used to have

local isAcceptable = false  
while(isAcceptable==false) do  
 x= math.random(0,\_W)  
 --check for overlap  
 if x \< lastItemX or x \> lastItemX+lastitemWidth then  
 isAcceptable = true  
 end  
end  

this will keep getting a new position till it is acceptable for you to use

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16321 reply_id: 60794[/import]

@GrahamRanson, JayantV

Thanks for your response now it makes sense and a sample code as posted
by JayantV helps out even more, now the problem im trying to work around
is how to implement that function in my current random spawn function
without get (a nil value error) can you post a sample how you will use that
in a randomSpawn function?

  • This is a sample code of how my current code works the difference in my
    code is I have a table with images inside table and each time you spawn
    an image will be something different from inside the table.

[lua]----- SPAWN RANDOM BALLOONS {1} -----
local function trackImg(obj)
obj:removeSelf()
end

local function spawnRandom()
rand = math.random( 300 )
local j

if (rand < 100) then

local j = display.newImage(“random.png”)
j:setReferencePoint(display.CenterReferencePoint)
j.x = math.random(35, 285)
j.y = math.random(0, 300)
j.myName = “j”
RandomGroup:insert(j)

elseif (rand < 200) then

local j = display.newImage(“random.png”)
j:setReferencePoint(display.CenterReferencePoint)
j.x = math.random(35, 285)
j.y = math.random(0, 300)
j.myName = “j”
RandomGroup:insert(j)

else

local j = display.newImage(“random.png”)
j:setReferencePoint(display.CenterReferencePoint)
j.x = math.random(35, 285)
j.y = math.random(0, 300)
j.myName = “j”
RandomGroup:insert(j)

end

end

– Round Balloon Timer –
local Drop = timer.performWithDelay(500, spawnRandom, 0)

[import]uid: 30314 topic_id: 16321 reply_id: 60893[/import]

Would anyone be kind enough to post a sample code on how to accomplish this:

1: Spawn “1” image every 1 second

2: when you touch image you remove it

3: Image should NOT spawn on top of each other or close enough to bump
into each other.

– The Sample Code i posted previously was just a code to make images
spawn randomly, Up to now on my code I have a table with different images
inside the table and randomly one of the images inside the table spawn every 1 second
and when you touch the image you remove it (so basically 1: and 2:) my PROBLEM
is 3: since the image x and y are done by math.random sometimes they spawn
on top of each other or close to each other and bump (since each image have
physics body and bounce ect) and on my game it makes it look un real and .isSensor
is not an option for my type of game.

So If i can make them spawn but always keep a small distance from each other that
would be great !

Any help is greatly appreciated, the help I got before my previous post were great
but unfortunately even if I understood what Im supposed to do I could not implement
it on my code so this is why i`m asking if anyone can please post a sample code on how
to accomplish this.! [import]uid: 30314 topic_id: 16321 reply_id: 63483[/import]

  1. timer.performWithDelay(1000,function() spawn() end,0)
    or
    timer.performWithDelay(1000,spawn,0)

  2. inside the spawn, add a function
    obj:addEventListener(“tap”,function() obj:removeSelf() end)

  3. save each object in a table and then check this object against all the objects in the table, if there is an overlap, get a new position or adjust the position slightly on the x/y

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16321 reply_id: 63485[/import]

@ JayantV

Thanks for reply, as I previously posted I got down #1 and #2 and code
working great my problem is #3 and even if you explain it I know why
my code is causing images to spawn on top of each other and close by
and I also know what needs to be done (what you explained) The problem
is I don`t know how to implement that code to fix it ;( I been learning
programming and Corona for a few months so I know a thing or two
but there still some things that even if I “know” how to fix I still cant
code it “properly” to fix it.

Can you provide a sample code on how you would combine all:
#1
#2
#3

Thanks in advance! [import]uid: 30314 topic_id: 16321 reply_id: 63517[/import]

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]

@ nick_sherman

Thanks for the response and help! I`ll test this code out in a few and
post the results but either way it helped me a lot by looking at it! [import]uid: 30314 topic_id: 16321 reply_id: 63634[/import]