problem with event closure

Code: http://pastie.org/1821516

In the snippet referenced above I’ve trimmed most everything out leaving what should be a fairly simple block of code showing my problem.

in game.lua I create the variable gameIsActive. After doing so I

Cannon.require(“cannon”)
Cannon.gameIsActive = gameIsActive

This gameIsActive variable is updated by a pause button; while gameIsActive == false transitions are paused, touch handlers shouldn’t work, etc.

The problem is that upon tapping the pause button, which is defined in game.lua where gameIsActive is initially declared, the value of gameIsActive within Cannon is never updated. I’m still able to fire projectiles as if the button was never pressed.

Do variables within closures not get updated like normal? Any other variables that I assign into Cannon in this manner are properly updated as they change elsewhere in the program. [import]uid: 50570 topic_id: 9194 reply_id: 309194[/import]

OK, let’s do a step-by-step analysis.

[lua]local gameIsActive = true
[…]
Cannon.gameIsActive = gameIsActive[/lua]
This sets “Cannon.gameIsActive” to true.

[lua]gameIsActive = false[/lua]
This sets the local “gameIsActive” to false. And that’s it. This has nothing to do with closures or the way they work.
But you’re thinking about this the wrong way. gameIsActive is a global property of your game. Just set it to global and you’re done:

[lua]game.lua:
– Logic variables
gameIsActive = true

– Import the Cannon and pass in required globals
local Cannon = require(“cannon”)

local drawHUD = function()
local onPauseTouch = function(event)
if event.phase == “release” then
audio.play(tapSound)

if gameIsActive then
gameIsActive = false
tm:pauseAll()
else
gameIsActive = true
tm:resumeAll()
end
end
end

local pauseButton = ui.newButton{
defaultSrc = “images/pausebutton.png”, defaultX = 46, defaultY = 46,
overSrc = “images/pausebutton-over.png”, overX = 46, overY = 46,
onEvent = onPauseTouch
}

pauseButton:setReferencePoint(display.BottomLeftReferencePoint)
pauseButton.x = 50; pauseButton.y = 320
GUI:insert(pauseButton)

– Place Cannons
for i = 0,14 do
local cannon = Cannon.newCannon(i)
foreground:insert(cannon)
cannons[cannon] = cannon
end
end

cannon.lua:
module(…, package.seeall)

– Create method for creating new cannons
function newCannon(index)
– snip snip snip –
function cannon:touch(event)
if event.phase == “ended” and gameIsActive then
– shoot ze pellets
end
end
– snip snip snip –
end[/lua] [import]uid: 51516 topic_id: 9194 reply_id: 33551[/import]

The problem is that that isn’t the case. If I set gameIsActive = false as a global in my main.lua, reference it in my cannon.lua, then later on update it to true in game.lua after requiring cannon.lua the value still gets treated as false in cannon.lua
[import]uid: 50570 topic_id: 9194 reply_id: 33588[/import]