Problem removing obj in array and event listeners attached

Hey Guys, Looking for some help as I’m a little stuck on how to execute the following code properly.

I am trying to establish a “gameOver” function in my game but am having problems 1. removing what appears to be remaining obj’s in my array and #2 the event listeners

Here is my code 

 --create the BUG's as random images of 3 different BUGS --local bugImages = {"beetle.png", "wasp.png", "bee.png"} --print( bugImages[math.random (#bugImages)] ) --local bug[TotalBugs] = display.newImage(bugImages[math.random (#bugImages)]) bug[TotalBugs] = display.newImage("bee.png", 35,35) --setup tab listener bug[TotalBugs]:addEventListener( "tap", bugKilled ) --setup where bug will spawn bug[TotalBugs].x = startingX bug[TotalBugs].y = startingY --physics.addBody( bug[TotalBugs], "dynamic", { isSensor=true, radius=35} ) bug[TotalBugs].name = "enemyBug" group:insert(bug[TotalBugs]) --move the bugs to the center of the screne --bug[TotalBugs].trans = transition.to(bug[TotalBugs],{ time=2000, x=centerX, y=centerY, onComplete=bugKilled}) bug[TotalBugs].trans = transition.to(bug[TotalBugs], { time=2000, x=centerX, y=centerY, onComplete = function (self) self.parent:remove(self); self = nil; -- Minus lives as has hit center and then determine if lives = 0 if so call activateGameOver() lives = lives - 1 if lives \<= 0 then --timer.cancel(tmr\_createBug) --self:removeEventListener( "tap", bugKilled ) print( "livesOver" ) end end; }) end

Here is the error I get (“screenshot attached”)

Aswell was wondering what the best way would be to load some images as you can see at the start of my function I try to reference a few bugs then when I create them that line dose not appear to work

Any help would be much appreciated guys.

I would suggest looking in your console log to see if there are more messages just before it or if the stack trace is longer.  It would also be helpful to know what lines these errors are happening on.

Rob

I will re-run the code in sublime in debugger mode and see what the console reads out and post it up here. Currently at work but will do it once I get home tonight. 

Thanks

Danny

Did a a log me in session and recorded the following via the console screen

2014-03-17 15:32:46.028 Corona Simulator[13887:507] Runtime error ?:0: attempt to index a nil value stack traceback: &nbsp;&nbsp;&nbsp;&nbsp;[C]: ? &nbsp;&nbsp;&nbsp;&nbsp;?: in function 'cancel' &nbsp;&nbsp;&nbsp;&nbsp;...ames/\_GAMES/Bug Splatter (temp) - GAME/gameScene.lua:173: in function \<...ames/\_GAMES/Bug Splatter (temp) - GAME/gameScene.lua:165\> &nbsp;&nbsp;&nbsp;&nbsp;(tail call): ? &nbsp;&nbsp;&nbsp;&nbsp;?: in function \<?:466\> &nbsp;&nbsp;&nbsp;&nbsp;?: in function \<?:218\> [Finished in 63.1s] &nbsp;

via using the following code.

--create the BUG's as random images of 3 different BUGS -- LINE 148 --local bugImages = {"beetle.png", "wasp.png", "bee.png"} --print( bugImages[math.random (#bugImages)] ) --local bug[TotalBugs] = display.newImage(bugImages[math.random (#bugImages)]) bug[TotalBugs] = display.newImage("bee.png", 35,35) --setup tab listener bug[TotalBugs]:addEventListener( "tap", bugKilled ) --setup where bug will spawn bug[TotalBugs].x = startingX bug[TotalBugs].y = startingY --physics.addBody( bug[TotalBugs], "dynamic", { isSensor=true, radius=35} ) bug[TotalBugs].name = "enemyBug" group:insert(bug[TotalBugs]) --move the bugs to the center of the screne --bug[TotalBugs].trans = transition.to(bug[TotalBugs],{ time=2000, x=centerX, y=centerY, onComplete=bugKilled}) bug[TotalBugs].trans = transition.to(bug[TotalBugs], { time=2000, x=centerX, y=centerY, onComplete = function (self) self.parent:remove(self); self = nil; -- Minus lives as has hit center and then determine if lives = 0 if so call activateGameOver() lives = lives - 1 if lives \<= 0 then timer.cancel(tmr\_createBug) self:removeEventListener( "tap", bugKilled ) print( "livesOver" ) end end; }) end -- LINE 178

Well if line 173 is the culprit, and line 173 is:  timer.cancel(tmr_createBug)

It’s unlikely that timer.cancel is nil unless you’ve overwritten the timer object by using a global variable named timer (a real possibility) so I’m guessing that tmr_createBug is nil.  This is more likely as you are probably creating the timer inside a function and making it local to that function. 

Rob

Thanks found the problem and an easy mistake it appears I made while coding late a night. I was referencing the tmr_createBugs as local in the below function, removed the local and its all now working and just set is as local in my main variables calls up the top.

function scene:enterScene( event ) local group = self.view -- INSERT code here (e.g. start timers, load audio, start listeners, etc.) physics.start() local tmr\_createBug = timer.performWithDelay(1000, createBugs, 0) --timer.performWithDelay( 8000, makeGameFaster, 2) end

any idea’s on why the lines 49-51 above would not be working Rob?

Well remember just removing the “local” makes it a global and there are inherent dangers to that.  It’s better to declare it local at the top of your module file if you’re going to do that.

As for your second questions, I don’t know what line’s 49-51 are. 

Rob

Yeah I have declared it as a local at the start of my module as per above. 

Basically I have 3-4 different display objs and as I spawnBug would like it to randomly select one of those bug images as per the below code. 

local bugImages = {"beetle.png", "wasp.png", "bee.png"} --print( bugImages[math.random (#bugImages)] ) local bug[TotalBugs] = display.newImage(bugImages[math.random (#bugImages)])

it appears I am doing something wrong it the above code will not work but looks logically correct,

You do not mention what actually happens.

My apologies Primoz I was not at my computer so therefore was unable to provide a readout of the error message, below is the error I receive when running in.

 --create the BUG's as random images of 3 different BUGS local bugImages = {"bee.png","beetle.png", "wasp.png"} local bug[TotalBugs] = display.newImage(bugImages[math.random (#bugImages)]) --bug[TotalBugs] = display.newImage("bee.png", 35,35) --setup tab listener bug[TotalBugs]:addEventListener( "tap", bugKilled ) --setup where bug will spawn bug[TotalBugs].x = startingX bug[TotalBugs].y = startingY --physics.addBody( bug[TotalBugs], "dynamic", { isSensor=true, radius=35} ) bug[TotalBugs].name = "enemyBug" group:insert(bug[TotalBugs])

File: error loading module 'gameScene' from file '/Users/danny\_roerring/Dropbox/Documents/Game Development - Roedan Games/\_GAMES/Bug Splatter (temp) - GAME/gameScene.lua' Error loading module 'gameScene' from file '/Users/danny\_roerring/Dropbox/Documents/Game Development - Roedan Games/\_GAMES/Bug Splatter (temp) - GAME/gameScene.lua': ...velopment - Roedan Games/\_GAMES/Bug Splatter (temp) - GAME/gameScene.lua:151: unexpected symbol near '[' stack traceback: [C]: ? [C]: in function 'error' ?: in function 'gotoScene' ...ames/\_GAMES/Bug Splatter (temp) - GAME/menuScene.lua:32: in function '\_onRelease' ?: in function '?' ?: in function \<?:1091\> ?: in function \<?:218\>

The problem is the local directive

local bug[TotalBugs]= display.newImage(bugImages[math.random (#bugImages)])

you can not make a table index local only the table can be made local where it si declared.

This will work:

bug[TotalBugs]= display.newImage(bugImages[math.random (#bugImages)])

OMG your correct, can’t believe I totally overlooked that and didn’t realise. Thanks for seeing it for me. Always good having another pair of eyes. Will correct this once I get home and re-test. 

I would suggest looking in your console log to see if there are more messages just before it or if the stack trace is longer.  It would also be helpful to know what lines these errors are happening on.

Rob

I will re-run the code in sublime in debugger mode and see what the console reads out and post it up here. Currently at work but will do it once I get home tonight. 

Thanks

Danny

Did a a log me in session and recorded the following via the console screen

2014-03-17 15:32:46.028 Corona Simulator[13887:507] Runtime error ?:0: attempt to index a nil value stack traceback: &nbsp;&nbsp;&nbsp;&nbsp;[C]: ? &nbsp;&nbsp;&nbsp;&nbsp;?: in function 'cancel' &nbsp;&nbsp;&nbsp;&nbsp;...ames/\_GAMES/Bug Splatter (temp) - GAME/gameScene.lua:173: in function \<...ames/\_GAMES/Bug Splatter (temp) - GAME/gameScene.lua:165\> &nbsp;&nbsp;&nbsp;&nbsp;(tail call): ? &nbsp;&nbsp;&nbsp;&nbsp;?: in function \<?:466\> &nbsp;&nbsp;&nbsp;&nbsp;?: in function \<?:218\> [Finished in 63.1s] &nbsp;

via using the following code.

--create the BUG's as random images of 3 different BUGS -- LINE 148 --local bugImages = {"beetle.png", "wasp.png", "bee.png"} --print( bugImages[math.random (#bugImages)] ) --local bug[TotalBugs] = display.newImage(bugImages[math.random (#bugImages)]) bug[TotalBugs] = display.newImage("bee.png", 35,35) --setup tab listener bug[TotalBugs]:addEventListener( "tap", bugKilled ) --setup where bug will spawn bug[TotalBugs].x = startingX bug[TotalBugs].y = startingY --physics.addBody( bug[TotalBugs], "dynamic", { isSensor=true, radius=35} ) bug[TotalBugs].name = "enemyBug" group:insert(bug[TotalBugs]) --move the bugs to the center of the screne --bug[TotalBugs].trans = transition.to(bug[TotalBugs],{ time=2000, x=centerX, y=centerY, onComplete=bugKilled}) bug[TotalBugs].trans = transition.to(bug[TotalBugs], { time=2000, x=centerX, y=centerY, onComplete = function (self) self.parent:remove(self); self = nil; -- Minus lives as has hit center and then determine if lives = 0 if so call activateGameOver() lives = lives - 1 if lives \<= 0 then timer.cancel(tmr\_createBug) self:removeEventListener( "tap", bugKilled ) print( "livesOver" ) end end; }) end -- LINE 178

Well if line 173 is the culprit, and line 173 is:  timer.cancel(tmr_createBug)

It’s unlikely that timer.cancel is nil unless you’ve overwritten the timer object by using a global variable named timer (a real possibility) so I’m guessing that tmr_createBug is nil.  This is more likely as you are probably creating the timer inside a function and making it local to that function. 

Rob

Thanks found the problem and an easy mistake it appears I made while coding late a night. I was referencing the tmr_createBugs as local in the below function, removed the local and its all now working and just set is as local in my main variables calls up the top.

function scene:enterScene( event ) local group = self.view -- INSERT code here (e.g. start timers, load audio, start listeners, etc.) physics.start() local tmr\_createBug = timer.performWithDelay(1000, createBugs, 0) --timer.performWithDelay( 8000, makeGameFaster, 2) end

any idea’s on why the lines 49-51 above would not be working Rob?

Well remember just removing the “local” makes it a global and there are inherent dangers to that.  It’s better to declare it local at the top of your module file if you’re going to do that.

As for your second questions, I don’t know what line’s 49-51 are. 

Rob

Yeah I have declared it as a local at the start of my module as per above. 

Basically I have 3-4 different display objs and as I spawnBug would like it to randomly select one of those bug images as per the below code. 

local bugImages = {"beetle.png", "wasp.png", "bee.png"} --print( bugImages[math.random (#bugImages)] ) local bug[TotalBugs] = display.newImage(bugImages[math.random (#bugImages)])

it appears I am doing something wrong it the above code will not work but looks logically correct,

You do not mention what actually happens.