Hi guys,
I’m trying to prevent spawning circles from overlapping. This is my code:
for i=1, math.random(1, 6) do radius = math.random(centerX \* 0.05, centerX \* 0.2) obstacles[i] = display.newCircle(centerX, centerY, radius) obstacles[i].x = math.random((centerX \* 0.1) + (radius), actualW - ((centerX \* 0.1) + (radius))) obstacles[i].y = math.random(centerY \* 0.425, actualH - centerY \* 0.25) obstacles[i].radius = radius local randomColor = math.random(1,2) if randomColor == 1 then obstacles[i]:setFillColor(26/255, 188/255, 156/255) else obstacles[i]:setFillColor(243/255, 156/255, 18/255) end obstacles[i].strokeWidth = radius \* 0.2 for j=1, #obstacles - 1 do if obstacles[i].x - obstacles[i].radius \<= obstacles[j].x and obstacles[i].x + obstacles[i].radius \>= obstacles[j].x then if obstacles[i].y - obstacles[i].radius \<= obstacles[j].y and obstacles[i].y + obstacles[i].radius \>= obstacles[j].y then i = i - 1 end end end end
So what I’m trying to do is loop through all the objects in the list and check their x and y positions. If the new object (obstacles[j]) is in between them, I decrement i by 1 so my for loop would do the loop over again and overwrite the current circle. This isn’t working for me for some reason. Does anyone see what’s wrong?