Hello, I’ve been trying to figure out on how can I access an updated variable. I have tried putting the variable “canSpawn” inside a Runtime event (enterFrame) so that I can easily manipulate the behavior of the code that depends on this variable. Here is the code:
scene_startGame.lua
[lua]-- on this part i have forward reference the variable canSpawn
local canSpawn
– then i initialize it as true on the start of the program
canSpawn = true
…some code
– handleSpawn function handles the “canSpawn” variable
–this is where I update my variable so that when it has reached its goal, the value of the variable would be updated to false
local function handleSpawn()
if passTable.timer.minuteHandler == 0 and passTable.timer.secondHandler == 0 and passTable.timer.milliHandler == 0 then
canSpawn = false
else
canSpawn = true
end
print(canSpawn)
return canSpawn
end
– i made this function as an enterFrame event listener so that it would check and update
– the variable inside this function every frame
Runtime:addEventListener( “enterFrame”, handleSpawn)
– Spawn
control.spawnControl = function()
gameVar.zombieGender = rand(1,2) – gender picker
if(gameVar.zombieGender == 1)then
gameVar.choice = ‘common’
else
gameVar.choice = ‘common_girl’
end
– this will inherit the zombie factory and the type of zombie to deploy
passTable.tZombies[#passTable.tZombies+1] = ZombieFactory:new( enemyLayer, gameVar.choice, 1 )
– the collider of zombie
passTable.tZombies[#passTable.tZombies]:collider()
end
– this is where the problem is located and the area i want to access the updated variable
– on this part of code, canSpawn is never updated
– even though handleSpawn is already a Runtime event ( that is “enterFrame” ) it still see handleSpawn as true
if handleSpawn() == true then
spawnTimer = timer.performWithDelay(totalTimer, control.spawnControl, 0)
elseif handleSpawn() == false then
print(“not working”)
–timer.pause(spawnTimer)
end
– Runtime Control
controlLoop = function ()
– on this area all of the things that should be updated every frame must be put inside this loop
local function updateLoop()
passTable.tUtility[#passTable.tUtility]:updatePlayerCoordinates(charGroup,passTable.tChar[#passTable.tChar].x, passTable.tChar[#passTable.tChar].contentBounds.yMin)
– well this is where canSpawn is updated
if handleSpawn() == true then
passTable.tWeapon[#passTable.tWeapon]:startFiring(passTable.tChar[#passTable.tChar].x, passTable.tChar[#passTable.tChar].contentBounds.yMin, charGroup)
end
control.carControl()
end
Runtime:addEventListener( “enterFrame”, updateLoop )
end[/lua]
