Hey Guys,
Looking at some guidance as to what could be causing my enemy[i] (array of enemies) and my collision listener to stop working when switching from my gamescene.lua to menuscene.lua and back to my gamescene.lua
I have the following code on both my enterScene and Exit Scene
function scene:enterScene( event ) local group = self.view -- INSERT code here (e.g. start timers, load audio, start listeners, etc.) --Setup Physics physics.start() physics.setDrawMode("normal") print("Total Bugs on Enter: " .. TotalBugs) --Create Bug timer tmr\_createBug = timer.performWithDelay(gameSpeed, createBugs, 0) --Setup Listeners Runtime:addEventListener( "collision", onCollision ) --Setup Sounds --Setup Level Variables score = 0 gameSpeed = 2000 bugsKilled = 0 juiceCollected = 0 ---- MAY NEED TO MOVE THIS AND CREATE MENY BACK TO ENTER SCENE lives = 10 TotalBugs = 0 --Create Game Menu createGameMenu(group) -- Create a timer that counts the elapsed seconds timeKeep = stopwatch.new() local function onFrame(event) survivalText.text = ("Survival: " .. timeKeep:toElapsedString()) end Runtime:addEventListener("enterFrame", onFrame) end
unction scene:exitScene( event ) local group = self.view -- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.) --Stop timers timer.cancel( tmr\_createBug ) transition.cancel("bugTransitions" ) menuActivated = false --Remove Listeners Runtime:removeEventListener("enterFrame", onFrame) Runtime:removeEventListener("collision", onCollision ) --Stop Physics physics.stop() --Remove Sounds for i = TotalBugs,1, -1 do if bug[i] ~= nil then bug[i]:removeEventListener( "tap", bugKilled ) transition.cancel( bug[i] ) display.remove( bug[i] ) bug[i] = nil end end print("Total Bugs: " .. TotalBugs) -- if group ~= nil then -- for i = group.numChildren, 1, -1 do -- group[i]:removeSelf( ) -- group[i] = nil -- end -- end -- Remove all Layers and Dsiplay Groups if gameOverLayer ~= nil then for l=gameOverLayer.numChildren,1,-1 do local child = gameOverLayer[l] child.parent:remove( child ) end end if gameLayer ~= nil then for o=gameLayer.numChildren,1,-1 do gameLayer[o]:removeSelf( ) gameLayer[o] = nil end end local removeAll; removeAll = function(group) if group.enterFrame then Runtime:removeEventListener("enterFrame", group); end if group.tap then group:removeEventListener("touch", group); Runtime:removeEventListener("touch", group); end for i = group.numChildren, 1, -1 do if group[i].numChildren then removeAll(group[i]); else if group[i].enterFrame then Runtime:removeEventListener("enterFrame", group[i]); end if group[i].tap then group[i]:removeEventListener("tap", group[i]); Runtime:removeEventListener("tap", group[i]); end end end end removeAll(group); end