random images

i have a couple of images displaying on my simulator and at times when i run it the images overlap just a little bit, how can i stopped them from overlapping each other?

Can you post some code?  A screen shot could be helpful too.

i dont have software to make screen shots , but just think of the circles display randomly and sometimes they overlap since they are randomly placed, i dont want any to be touching

display.setStatusBar(display.HiddenStatusBar)


local x = display.contentWidth

local y = display.contentHeight


local myTable = {}


function dots()

myTable[1] = display.newImage(“yellowGreen.png”)

myTable[2] = display.newImage(“pinkDot.png”)

myTable[3] = display.newImage(“orange.png”)

myTable[4] = display.newImage(“blue.png”)



for i=1,#myTable do

myTable[i].x= math.random(30,x-30)

myTable[i].y=math.random(30,y-30)

end


return true

end


local displayDots = timer.performWithDelay( 100,dots)

No soft needed for screen shots. On keyboard there is PrintScreen button.

your

myTable[i].x= math.random(30,x-30) myTable[i].y=math.random(30,y-30)

is the same as

myTable[i].x= math.random(30,nil-30) myTable[i].y=math.random(30,nil-30)

because x and y variable is nowhere definied, so math.random probably gives strange results.

Think as if you would have few images on your (real) desktop and how would you arrange them to not overlap. Then apply what you got in corona

If you’re just using random numbers, then there is really no way to guarantee that they won’t overlap … at best, you could check for the closest neighbor and adjust the new circle’s radius to be less than that distance.  Otherwise you’d need some kind of outer loop to look at all circles and check for overlap with all others

(as a completely “out-there” idea, and depending on what else you’re trying to do, you might be able to fudge something with a physics environment:  physics.addBody for each object and then let them bump into each other to separate themselves out … if you set the density, etc. of the objects right, they might separate themselves out very quickly, but it would not be instantaneous, it would be visible to the user)

thanks for the replys, x and y are defined, at the top local x and local y, isnt that how you define, sorry im so new i dont know if thats what your talking about, your mistake or mine?

jbp1 is right.  Since you are randomly placing the images on the screen they may or may not overlap.  You would need some kind of code to check if there are any other images in the same location.  Or, as he advised, maybe use physics if you are already doing so for other parts of the game.

thanks

JoelG – while you do define x and y, I don’t think they are visible when the ‘dots’ function runs.  While I’m not a Lua expert, I think that since you’re calling ‘dots’ through a timer, the function is actually called by the runtime … and those local (to the file) variables x and y are not part of the runtime.

Yep, my mistake. @jbp1 they will be seen - are in scope

This may seem a bit off topic, but this blog post on non-physics collisions can help with this problem:
 

http://www.coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

When you try to place your new item, loop over your existing items to make sure it’s not colliding with another object.

Can you post some code?  A screen shot could be helpful too.

i dont have software to make screen shots , but just think of the circles display randomly and sometimes they overlap since they are randomly placed, i dont want any to be touching

display.setStatusBar(display.HiddenStatusBar)


local x = display.contentWidth

local y = display.contentHeight


local myTable = {}


function dots()

myTable[1] = display.newImage(“yellowGreen.png”)

myTable[2] = display.newImage(“pinkDot.png”)

myTable[3] = display.newImage(“orange.png”)

myTable[4] = display.newImage(“blue.png”)



for i=1,#myTable do

myTable[i].x= math.random(30,x-30)

myTable[i].y=math.random(30,y-30)

end


return true

end


local displayDots = timer.performWithDelay( 100,dots)

No soft needed for screen shots. On keyboard there is PrintScreen button.

your

myTable[i].x= math.random(30,x-30) myTable[i].y=math.random(30,y-30)

is the same as

myTable[i].x= math.random(30,nil-30) myTable[i].y=math.random(30,nil-30)

because x and y variable is nowhere definied, so math.random probably gives strange results.

Think as if you would have few images on your (real) desktop and how would you arrange them to not overlap. Then apply what you got in corona

If you’re just using random numbers, then there is really no way to guarantee that they won’t overlap … at best, you could check for the closest neighbor and adjust the new circle’s radius to be less than that distance.  Otherwise you’d need some kind of outer loop to look at all circles and check for overlap with all others

(as a completely “out-there” idea, and depending on what else you’re trying to do, you might be able to fudge something with a physics environment:  physics.addBody for each object and then let them bump into each other to separate themselves out … if you set the density, etc. of the objects right, they might separate themselves out very quickly, but it would not be instantaneous, it would be visible to the user)

thanks for the replys, x and y are defined, at the top local x and local y, isnt that how you define, sorry im so new i dont know if thats what your talking about, your mistake or mine?

jbp1 is right.  Since you are randomly placing the images on the screen they may or may not overlap.  You would need some kind of code to check if there are any other images in the same location.  Or, as he advised, maybe use physics if you are already doing so for other parts of the game.

thanks

JoelG – while you do define x and y, I don’t think they are visible when the ‘dots’ function runs.  While I’m not a Lua expert, I think that since you’re calling ‘dots’ through a timer, the function is actually called by the runtime … and those local (to the file) variables x and y are not part of the runtime.

Yep, my mistake. @jbp1 they will be seen - are in scope