Spawn obstacles constantly with a function and a timer

Hi you all, I’m having troubles when I try to spawn objects in my game, now I’m using the following code:

local ob = display.newRect(\_W, \_H/2-160, 10, math.random(20, 140)) physics.addBody( ob, "static", { friction=0.5, bounce=0.0 } ) ob.isFixedRotation = true ob:setFillColor(255, 0, 0) localGroup:insert(ob) local obD = display.newRect(\_W, \_H/2+160, 10, math.random(-140, -20)) physics.addBody(obD, "static", { density = 0, friction = 0.0, bounce = 0 } ) obD.isFixedRotation = true obD:setFillColor(255, 0, 0) localGroup:insert(obD) local function moveOb() ob.x = (ob.x or 0) - waveSpeed end local function moveObD() obD.x = (obD.x or 0) - waveSpeed end tmrMoveOb = timer.performWithDelay(1, moveOb, 0) tmrMoveObD = timer.performWithDelay(1, moveObD, 0)

Now, what should I do If I want to spawn two object every 1500 milliseconds?

I tried

local function spawnOb() local ob = display.newRect(\_W, \_H/2-160, 10, math.random(20, 140)) physics.addBody( ob, "static", { friction=0.5, bounce=0.0 } ) ob.isFixedRotation = true ob:setFillColor(255, 0, 0) localGroup:insert(ob) local function moveOb() ob.x = (ob.x or 0) - 1 end tmrMoveOb = timer.performWithDelay(1, moveOb, 0) end tmrSpawnOb = timer.performWithDelay(1500, spawnOb, 0) local function spawnObD() local obD = display.newRect(\_W, \_H/2+160, 10, math.random(-140, -20)) physics.addBody(obD, "static", { density = 0, friction = 0.0, bounce = 0 } ) obD.isFixedRotation = true obD:setFillColor(255, 0, 0) --localGroup:insert(obD) local function moveObD() obD.x = (obD.x or 0) - 1 end tmrMoveObD = timer.performWithDelay(1, moveObD, 0) end tmrSpawnObD = timer.performWithDelay(1500, spawnObD, 0) --START: SETTING SCORE local score = 0 local secDisplay = "" local distance = 100 local myText = display.newText("", \_W-30, 310, "PUSAB", 8) localGroup:insert(myText) myText:setTextColor(255, 255, 255) local function updateScore() score = score + 1 myText.text = secDisplay..score end myTimer = timer.performWithDelay(distance, updateScore, 0)

but in this way my obstacles become in foreground (covering the score counter) .

What could I do to fix this issue? Thanks! :slight_smile:

Create 2 display groups - last one will be on top. Insert score into second one and objects to first one. This way objects will be always under the score

Hi, thanks for your answer, I tried to code

local localGroup = display.newGroup() local scoreGroup = display.newGroup() local function spawnOb() local ob = display.newRect(\_W, \_H/2-160, 10, math.random(20, 140)) physics.addBody( ob, "static", { friction=0.5, bounce=0.0 } ) ob.isFixedRotation = true ob:setFillColor(255, 0, 0) localGroup:insert(ob) local function moveOb() ob.x = (ob.x or 0) - 1 end tmrMoveOb = timer.performWithDelay(1, moveOb, 0) end tmrSpawnOb = timer.performWithDelay(1500, spawnOb, 0) local function spawnObD() local obD = display.newRect(\_W, \_H/2+160, 10, math.random(-140, -20)) physics.addBody(obD, "static", { density = 0, friction = 0.0, bounce = 0 } ) obD.isFixedRotation = true obD:setFillColor(255, 0, 0) --localGroup:insert(obD) local function moveObD() obD.x = (obD.x or 0) - 1 end tmrMoveObD = timer.performWithDelay(1, moveObD, 0) end tmrSpawnObD = timer.performWithDelay(1500, spawnObD, 0) local score = 0 local secDisplay = "" local distance = 100 local myText = display.newText("", \_W-30, 310, "PUSAB", 8) scoreGroup:insert(myText) myText:setTextColor(255, 255, 255) local function updateScore() score = score + 1 myText.text = secDisplay..score end myTimer = timer.performWithDelay(distance, updateScore, 0) return localGroup return scoreGroup

but it returns an error in the “director class”. 

Any idea? Is my code correct?

Thanks again! :slight_smile:

Create 2 display groups - last one will be on top. Insert score into second one and objects to first one. This way objects will be always under the score

Hi, thanks for your answer, I tried to code

local localGroup = display.newGroup() local scoreGroup = display.newGroup() local function spawnOb() local ob = display.newRect(\_W, \_H/2-160, 10, math.random(20, 140)) physics.addBody( ob, "static", { friction=0.5, bounce=0.0 } ) ob.isFixedRotation = true ob:setFillColor(255, 0, 0) localGroup:insert(ob) local function moveOb() ob.x = (ob.x or 0) - 1 end tmrMoveOb = timer.performWithDelay(1, moveOb, 0) end tmrSpawnOb = timer.performWithDelay(1500, spawnOb, 0) local function spawnObD() local obD = display.newRect(\_W, \_H/2+160, 10, math.random(-140, -20)) physics.addBody(obD, "static", { density = 0, friction = 0.0, bounce = 0 } ) obD.isFixedRotation = true obD:setFillColor(255, 0, 0) --localGroup:insert(obD) local function moveObD() obD.x = (obD.x or 0) - 1 end tmrMoveObD = timer.performWithDelay(1, moveObD, 0) end tmrSpawnObD = timer.performWithDelay(1500, spawnObD, 0) local score = 0 local secDisplay = "" local distance = 100 local myText = display.newText("", \_W-30, 310, "PUSAB", 8) scoreGroup:insert(myText) myText:setTextColor(255, 255, 255) local function updateScore() score = score + 1 myText.text = secDisplay..score end myTimer = timer.performWithDelay(distance, updateScore, 0) return localGroup return scoreGroup

but it returns an error in the “director class”. 

Any idea? Is my code correct?

Thanks again! :slight_smile: