gameIsActive Boolean Problem!

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

Are you creating a closure with your onCollision function in a function ? I guess the problem is that with gameIsActive false (supposedly) it is running the timer.performWithDelay() stuff ; have you printed out gameIsActive at the top of the for loop.

It is neat in some ways to put functions in functions in this way, but it can create scoping issues.

Thanks for the reply. I’ve now taken the onCollision function and placed it just underneath the spawnTurtle function so that they’re not nested anymore. I’ve also place the “if(gameIsActive == true) then” above the for loop. Unfortunately when a collision occurs, the function is called, however, the variable gameIsActive still does not change to false and so the spawn function keeps getting called.

Is it a local or global ? Does the timer function still create a closure ?

Are you creating a closure with your onCollision function in a function ? I guess the problem is that with gameIsActive false (supposedly) it is running the timer.performWithDelay() stuff ; have you printed out gameIsActive at the top of the for loop.

It is neat in some ways to put functions in functions in this way, but it can create scoping issues.

Thanks for the reply. I’ve now taken the onCollision function and placed it just underneath the spawnTurtle function so that they’re not nested anymore. I’ve also place the “if(gameIsActive == true) then” above the for loop. Unfortunately when a collision occurs, the function is called, however, the variable gameIsActive still does not change to false and so the spawn function keeps getting called.

Is it a local or global ? Does the timer function still create a closure ?