Trouble randomizing enemy placement...

Trying to figure out the best way to add my obstacle to an endless runner style game.

When I drop the obstacle it’s using the size of the display plus 100.  

The problem is how do I drop another obstacle once I know my previous obstacle is on screen.  At the moment if I add a random number to the equation I sometimes have one obstacle dropping onto another which isn’t good. How can I check whether my current obstacle is on screen (i.e obstacle.x is < display.contentWidth + 100).  I’m also using storyboard if that is on any consequence.

I think I fixed my issue (only took me all day!) here’s what I did…

local prevXPos = 0 local randDistance = math.random(200,1000) function findXPosition() for i = 1, elements.numChildren, 1 do -- iterate through all the objects in the displaygroup prevXPos = elements[i].x -- last object in the displaygroup (elements) passes value of x position end end if (prevXPos ~= nil) then if (prevXPos \<= offScreen) then Obstacle.x = offScreen + randDistance Obstacle.y = display.viewableContentHeight - 152 physics.addBody(Obstacle, "dynamic", {density=1, bounce=0.1, friction=.2}) Obstacle.isFixedRotation = true Obstacle:play() elements:insert(Obstacle) end end findXPosition()

This seems to be working as far as I can tell, if anyone sees anything wrong let me know! Thanks! :smiley:

I think I fixed my issue (only took me all day!) here’s what I did…

local prevXPos = 0 local randDistance = math.random(200,1000) function findXPosition() for i = 1, elements.numChildren, 1 do -- iterate through all the objects in the displaygroup prevXPos = elements[i].x -- last object in the displaygroup (elements) passes value of x position end end if (prevXPos ~= nil) then if (prevXPos \<= offScreen) then Obstacle.x = offScreen + randDistance Obstacle.y = display.viewableContentHeight - 152 physics.addBody(Obstacle, "dynamic", {density=1, bounce=0.1, friction=.2}) Obstacle.isFixedRotation = true Obstacle:play() elements:insert(Obstacle) end end findXPosition()

This seems to be working as far as I can tell, if anyone sees anything wrong let me know! Thanks! :smiley: