Hello all,
I’ve been trying to incorporate a collision function and have been successful thus far, however, the only problem I’m encountering is when I try to set the “gameIsActive” boolean to "false when the collision function is called. For some reason it does not change the “if(gameIsActive == true) then” for the spawn function.
I would like to stop the spawn function from spawning more objects once there is a collision. How would this be possible?
local function spawnTurtle(params) turtle = display.newImage(params.image) turtle.objTable = params.objTable turtle.index = #turtle.objTable + 1 turtle.myName = "Object : " .. turtle.index turtle.objTable[turtle.index] = turtle turtle.x = math.random(0, display.contentWidth - turtle.contentWidth) physics.addBody(turtle, turtle.bodyType) turtle.touch = onTouch turtle:addEventListener("touch", turtle) local function onCollision(event) if(event.phase == "began") then gameIsActive = false print(gameIsActive) print("hi") end end Runtime:addEventListener("collision", onCollision) return turtle end local spawnTable = {} delay = 0 for i = 1, 100 do if(gameIsActive == true) then timer.performWithDelay(delay, function() local spawns = spawnTurtle( { image = "turtle.png", objTable = spawnTable }) end) delay = delay + 1000 print(gameIsActive) end print(gameIsActive) end for i = 1, #spawnTable do print(spawnTable[i].myName) end end
Thanks,
Angus