Storyboard listener problem

I currently have a simple game, a “menu.lua” and “level1.lua”. When I go to level 1 for the first time, everything works fine, but when I exit to the menu and then return, it does not work properly. The problem is that the “addEventListener” don’t seem to work. I have all my listeners in the enterScene function as follows:

[lua]function scene:enterScene( event )

local group = self.view

rocket:addEventListener(“collision”, on_hit)
screen:addEventListener(“touch”, touchrocket)
slow_btn:addEventListener(“touch”, slow)
speed_btn:addEventListener(“touch”, speed)

stopwatch.position_timer = timer.performWithDelay(200, check_position, 0)
stopwatch.move_timer = timer.performWithDelay(1500, move_slides, 0)
stopwatch.runtimer = timer.performWithDelay(10, runtime, 0)

end[/lua]

When I switch to the menu screen I call [lua]storyboard.removeScene(“level1”) [/lua] but that still does not help

Are you removing them in the exitScene()?

Are any of them local only to createScene()?

Rob

Yes I remove them all in exitScene, and they are all created in createScene, but none of them are local to it.

What I don’t understand is I thought that if you change scene, remove the old scene then return to it, it is as if you were visiting it for the first time? Why would something work the first time but not the second?

Without seeing more of your code, where are you calling storyboard.removeScene(“level1”)?  In level1.lua?  in menu.lua?

Removing a scene while it’s still the active scene is problematic.  I personally prefer to call removeScene() just before I call gotoScene() to make sure I’m removing the scene I’m going to.

But as I posted in another thread, there is a reason we try to keep these around.  Loading modules is an expensive process.  Creating the scene are an expensive as well.  If we can keep them in memory, your app will perform better.  However you have to take responsibility to do the right things at the right time.

We covered those steps in this tutorial:  http://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

Basically to summarize:  createScene() is used for creating the objects, but since it’s not guaranteed to run all the time, you can’t depend on it to reset.

willEnterScene() is a great place to reset your level.  You reset values back to their defaults.  Move objects back to their beginning position.  It’s some work, but its the most CPU efficient thing to do.  The scene isn’t on screen, so moving things around won’t show up.

enterScene() is where you create any native.* objects, start timers and transitions and start scene specific audio, since the scene is on the screen.

exitScene() undo anything you did in createScene().

Rob

Are you removing them in the exitScene()?

Are any of them local only to createScene()?

Rob

Yes I remove them all in exitScene, and they are all created in createScene, but none of them are local to it.

What I don’t understand is I thought that if you change scene, remove the old scene then return to it, it is as if you were visiting it for the first time? Why would something work the first time but not the second?

Without seeing more of your code, where are you calling storyboard.removeScene(“level1”)?  In level1.lua?  in menu.lua?

Removing a scene while it’s still the active scene is problematic.  I personally prefer to call removeScene() just before I call gotoScene() to make sure I’m removing the scene I’m going to.

But as I posted in another thread, there is a reason we try to keep these around.  Loading modules is an expensive process.  Creating the scene are an expensive as well.  If we can keep them in memory, your app will perform better.  However you have to take responsibility to do the right things at the right time.

We covered those steps in this tutorial:  http://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

Basically to summarize:  createScene() is used for creating the objects, but since it’s not guaranteed to run all the time, you can’t depend on it to reset.

willEnterScene() is a great place to reset your level.  You reset values back to their defaults.  Move objects back to their beginning position.  It’s some work, but its the most CPU efficient thing to do.  The scene isn’t on screen, so moving things around won’t show up.

enterScene() is where you create any native.* objects, start timers and transitions and start scene specific audio, since the scene is on the screen.

exitScene() undo anything you did in createScene().

Rob