Error scene changing

So I have my code set up to go to a certain scene if a variable reaches a certain number:

 function flylistener()   if spawnedflies \> 15 then    storyboard.gotoScene( "gameover" )   else  end  end

I call that function every 5 seconds, it works fine.

In the exitscene listener I have the following code:

-- Called when scene is about to move offscreen: function scene:exitScene( event )  local group = self.view  local flygroup = self.view    physics.stop()    timer.cancel(listenertimer)  listenertimer = nil  timer.cancel(spawntimer)  spawntimer = nil    spawn = nil  buttonListener = nil  listener = nil  spawner = nil  spawn = nil  flylistener = nil   end

I have this to try to cancel the timers (which are set to said variable names) then unload the variables, plus assign nil to all the functions I have set up. However, when the exitscene is called (when the gameover scene is called), then it tells me that timer.cancel is trying to index a nil value. Any idea why it won’t cancel?

EDIT: It would appear that IE removed the proper code formatting. Apologies.

First of all, you don’t need to nil your functions and if you come back to this scene it could cause problems.

If your timer is not running, there is a good chance that the handle will be nil.  If it’s already nil, then you don’t need to cancel it.  Typically you would wrap it with a check like this:

if myTimer then

    timer.cancel(myTimer)

    myTimer = nil

end

You could also have a scope problem, like having the timer you are trying to cancel local to some other function.

Rob

Thanks Rob, I’ll give it a try. While I’m on the subject, am I supposed to assign nil to all the variables and widgets/display objects I’ve created under destroyScene?

It’s a good practice.  Somethings need nilled because they set the reference count to zero on the memory so it releases.  Other times you’re doing it to take the remnant table and freeing the Lua memory that table is tying up.  Simple variables like numbers and strings don’t need it, but it doesn’t hurt.

Rob

First of all, you don’t need to nil your functions and if you come back to this scene it could cause problems.

If your timer is not running, there is a good chance that the handle will be nil.  If it’s already nil, then you don’t need to cancel it.  Typically you would wrap it with a check like this:

if myTimer then

    timer.cancel(myTimer)

    myTimer = nil

end

You could also have a scope problem, like having the timer you are trying to cancel local to some other function.

Rob

Thanks Rob, I’ll give it a try. While I’m on the subject, am I supposed to assign nil to all the variables and widgets/display objects I’ve created under destroyScene?

It’s a good practice.  Somethings need nilled because they set the reference count to zero on the memory so it releases.  Other times you’re doing it to take the remnant table and freeing the Lua memory that table is tying up.  Simple variables like numbers and strings don’t need it, but it doesn’t hurt.

Rob