Storyboard Transitions: HELP

Hey guys, in the game I’m making, I’m running into some difficulty. First I have a “menu” then I have my “game” file. The transition from menu to game works flawlessly. However, it is difficult to transfer from game to menu. Whenever I tell it to transfer from game to the menu, the objects from “game” stay on the screen, and the menu comes up. My game file creates all of its objects and listeners using individual functions that are called upon in createScene. How do I make storyboard remove all of these objects? Thanks!

ALSO: Yes, I do have removeAll() on the menu.

EDIT: To test if this was also happening on interactions between other screens, I added a back button on my menu screen that goes to the splash screen. The words/buttons from the menu screen stayed on the splash screen… I think I’m doing something wrong.

Did you insert all graphic objects into the scene view?

For example,

function scene:createScene(event) local group = self.view local textObj = display.newText(...) group:insert(textObj) end

If yes, probably you need to show some of your source code for further investigation. Storyboard transition is quite basic & solid in Corona, so you must be doing something wrong, definitely. :slight_smile:

storyboard.removeAll() -- local forward references should go here -- --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view local loadButton = display.newText( "Play", 0, 0, native.systemFont, 120 ) loadButton.x = display.contentCenterX loadButton.y = display.contentCenterY local backbutton = display.newText( "Go Back", 0, 0, native.systemFont, 120) backbutton.x = display.contentCenterX backbutton. y = display.viewableContentHeight \* 0.7 local function onTap( event ) storyboard.gotoScene( "scene\_game" ) end loadButton:addEventListener( "tap", onTap ) --local function goback( event) --storyboard.gotoScene( "scene\_splash" ) -- end --backbutton:addEventListener( "tap", goback ) group:insert( loadButton ) end -- Called BEFORE scene has moved onscreen: function scene:willEnterScene( event ) local group = self.view end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view end -- Called AFTER scene has finished moving offscreen: function scene:didExitScene( event ) local group = self.view end -- Called prior to the removal of scene's "view" (display view) function scene:destroyScene( event ) local group = self.view end -- Called if/when overlay scene is displayed via storyboard.showOverlay() function scene:overlayBegan( event ) local group = self.view local overlay\_name = event.sceneName -- name of the overlay scene end -- Called if/when overlay scene is hidden/removed via storyboard.hideOverlay() function scene:overlayEnded( event ) local group = self.view local overlay\_name = event.sceneName -- name of the overlay scene end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "willEnterScene" event is dispatched before scene transition begins scene:addEventListener( "willEnterScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "didExitScene" event is dispatched after scene has finished transitioning out scene:addEventListener( "didExitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) -- "overlayBegan" event is dispatched when an overlay scene is shown scene:addEventListener( "overlayBegan", scene ) -- "overlayEnded" event is dispatched when an overlay scene is hidden/removed scene:addEventListener( "overlayEnded", scene ) --------------------------------------------------------------------------------- return scene

Above is scene_menu.lua

Below is scene_game.lua

--------------------------------------------------------------------------------- -- game -- Scene notes go here --------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() storyboard.purgeOnSceneChange = true local physics = require( "physics" ) physics.start() physics.setGravity( 0, T ) -- Clear previous scene storyboard.removeAll() W = display.viewableContentWidth H = display.viewableContentHeight GStart = 0 Score = 0 -- local forward references should go here -- -------------------------------------- --HIGH SCORE STUFF-- -------------------------------------- path = system.pathForFile( "Score.txt" ) local scorepath = io.open( path, "r" ) local highest = scorepath:read( "\*a" ) local highscore = tonumber(highest) --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- function gamecreate() local foo = display.newImageRect("foo.png", 150, 150); foo.x = display.contentCenterX foo.y = display.contentCenterY local ground = display.newImageRect("ground.png", display.viewableContentWidth, display.viewableContentHeight \* 0.2); ground.x = display.contentCenterX ground.y = display.viewableContentHeight physics.addBody( foo, "static", {density=0, friction=0.5, bounce=0.5, filter = {maskBits = 4, categoryBits = 2}}) physics.addBody( ground, "static", {density=2, friction=0.5, bounce = 0.6, filter = {maskBits = 4, categoryBits = 2}}) local function drag( event ) if event.phase == "began" then foo.markX = foo.x --Storing X location foo.markY = foo.y --Storing Y location elseif event.phase == "moved" then local x = (event.x - event.xStart) + foo.markX local y = (event.y - event.yStart) + foo.markY foo.x, foo.y = x, y end return true end foo:addEventListener("touch",drag); GStart = GStart + 1 foo.collision = onLocalCollision foo:addEventListener( "collision", j ) end function snowing() meh = math.random( 0, 100 ) if (meh \< 33) then j = display.newImage("bsnow.png"); j.x = math.random( 1, display.viewableContentWidth ) j.y = -50 --j.rotation = math.random( -270, 270 ) physics.addBody( j, { density=0, friction=0.3, bounce=0.4, filter = {maskBits = 2, categoryBits = 4}} ) --, filter = {maskBits = 2, categoryBits = 4} elseif (meh \> 66) then j = display.newImage("gsnow.png"); j.x = math.random( 1, display.viewableContentWidth ) j.y = -50 --j.rotation = math.random( -270, 270 ) physics.addBody( j, { density=0, friction=0.3, bounce=0.2, filter = {maskBits = 2, categoryBits = 4}} ) --, filter = {maskBits = 2, categoryBits = 4} else j = display.newImage("snow.png"); j.x = math.random( 1, display.viewableContentWidth ) j.y = -50 --j.rotation = math.random( -270, 270 ) physics.addBody( j, { density=0, friction=0.2, bounce=0.5, filter = {maskBits = 2, categoryBits = 4}} ) end return lol end function addition() Score = Score + 1 end function onLocalCollision() timer.cancel( snownow ) timer.cancel( count ) local gameover = display.newText("You Lost!", W \* 0.5, H \* 0.5, "Helvetica", 100); gameover.x = W \* 0.5 gameover.y = H \* 0.2 local showmyhigh = display.newText( "Your high score is:", W \* 0.5, H \* 0.7, "Helvetica", 100); showmyhigh.x = W \* 0.5 showmyhigh.y = H \* 0.8 local showmyhigh2 = display.newText( highscore, W \* 0.5, H \* 0.7, "Helvetica", 100); showmyhigh2.x = W \* 0.5 showmyhigh2.y = showmyhigh.y + 20 local showscore = display.newText( Score , W \* 0.5, H \* 0.7, "Helvetica", 100); showscore.x = W \* 0.5 showscore.y = H \* 0.6 local replay = display.newImage( "replay.png" ) replay.x = W \* 0.2 replay.Y = H \* 0.5 local function tapped( event ) storyboard.gotoScene( "scene\_splash" ) end replay:addEventListener( "tap", tapped ) if highscore \< Score then local wow = display.newText( "High Score!", W \* 0.5, H \* 0.7, "Helvetica", 100) wow.x = W \* 0.5 wow.y = H \* 0.4 local path = system.pathForFile( "Score.txt") local file = io.open( path, "w+" ) file:write( Score ) end end -- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view gamecreate() count = timer.performWithDelay( 1000, addition, - 1) if GStart == 1 then snownow = timer.performWithDelay( 300, snowing, -1) end end -- Called BEFORE scene has moved onscreen: function scene:willEnterScene( event ) local group = self.view end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view end -- Called AFTER scene has finished moving offscreen: function scene:didExitScene( event ) local group = self.view end -- Called prior to the removal of scene's "view" (display view) function scene:destroyScene( event ) local group = self.view end -- Called if/when overlay scene is displayed via storyboard.showOverlay() function scene:overlayBegan( event ) local group = self.view local overlay\_name = event.sceneName -- name of the overlay scene end -- Called if/when overlay scene is hidden/removed via storyboard.hideOverlay() function scene:overlayEnded( event ) local group = self.view local overlay\_name = event.sceneName -- name of the overlay scene end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "willEnterScene" event is dispatched before scene transition begins scene:addEventListener( "willEnterScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "didExitScene" event is dispatched after scene has finished transitioning out scene:addEventListener( "didExitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) -- "overlayBegan" event is dispatched when an overlay scene is shown scene:addEventListener( "overlayBegan", scene ) -- "overlayEnded" event is dispatched when an overlay scene is hidden/removed scene:addEventListener( "overlayEnded", scene ) --------------------------------------------------------------------------------- return scene

So in your createScene() in “scene_game.lua”, you call 

gamecreate()

and I don’t see you have inserted your display objects into the scene’s view.

Isn’t it just what I said in my last post?

So should I simply cut all of the functions and turn them into local functions inside the scene’s view, then call them?

When you put:   storyboard:removeAll() at the top of a scene, if that scene is already loaded, it won’t execute again.  Code in the main chunk executes only on initial load.

What I like to do is remove the scene just before I go to it if I need to force a complete reset. 

storyboard.removeScene(“menu”)

storyboard.gotoScene(“menu”)

Keep in mind this is a harsh practice because loading a module and creating your display resources over again is an expensive operation. 

It’s better for you to learn how to properly reset a scene:  

Use createScene() to create objects, but for things that move, don’t expect createScene to be the authority on it. 

Use a willEnterScene() event to reset scene values and move things to their starting positions.

Use enterScene() to start any timers and transitions, any audio that needs to start once the scene is on the screen.  Also create an native.* objects here. 

Use exitScene to undo anything any thing you did in enterScene()

Rarely will you need to do things in didExitScene() or destroyScene(), though if you load any audio in createScene() you will want to dispose it in destoryScene().

There are many ways to do something.

In your gamecreate() function, you can simply add display objects to “scene.view” (which is self.view in createScene()). Or pass self.view to gamecreate() as a parameter so it can be used inside the function.

Or, you can create a display group (& insert into scene’s view) and then all your display objects are inserted into this display group.

There are just too many possibilities. The principle in your case is that you have to insert all graphic objects into the scene’s view no matter “directly” or “indirectly” in order for the storyboard to take care of your display objects.

Did you insert all graphic objects into the scene view?

For example,

function scene:createScene(event) local group = self.view local textObj = display.newText(...) group:insert(textObj) end

If yes, probably you need to show some of your source code for further investigation. Storyboard transition is quite basic & solid in Corona, so you must be doing something wrong, definitely. :slight_smile:

storyboard.removeAll() -- local forward references should go here -- --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view local loadButton = display.newText( "Play", 0, 0, native.systemFont, 120 ) loadButton.x = display.contentCenterX loadButton.y = display.contentCenterY local backbutton = display.newText( "Go Back", 0, 0, native.systemFont, 120) backbutton.x = display.contentCenterX backbutton. y = display.viewableContentHeight \* 0.7 local function onTap( event ) storyboard.gotoScene( "scene\_game" ) end loadButton:addEventListener( "tap", onTap ) --local function goback( event) --storyboard.gotoScene( "scene\_splash" ) -- end --backbutton:addEventListener( "tap", goback ) group:insert( loadButton ) end -- Called BEFORE scene has moved onscreen: function scene:willEnterScene( event ) local group = self.view end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view end -- Called AFTER scene has finished moving offscreen: function scene:didExitScene( event ) local group = self.view end -- Called prior to the removal of scene's "view" (display view) function scene:destroyScene( event ) local group = self.view end -- Called if/when overlay scene is displayed via storyboard.showOverlay() function scene:overlayBegan( event ) local group = self.view local overlay\_name = event.sceneName -- name of the overlay scene end -- Called if/when overlay scene is hidden/removed via storyboard.hideOverlay() function scene:overlayEnded( event ) local group = self.view local overlay\_name = event.sceneName -- name of the overlay scene end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "willEnterScene" event is dispatched before scene transition begins scene:addEventListener( "willEnterScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "didExitScene" event is dispatched after scene has finished transitioning out scene:addEventListener( "didExitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) -- "overlayBegan" event is dispatched when an overlay scene is shown scene:addEventListener( "overlayBegan", scene ) -- "overlayEnded" event is dispatched when an overlay scene is hidden/removed scene:addEventListener( "overlayEnded", scene ) --------------------------------------------------------------------------------- return scene

Above is scene_menu.lua

Below is scene_game.lua

--------------------------------------------------------------------------------- -- game -- Scene notes go here --------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() storyboard.purgeOnSceneChange = true local physics = require( "physics" ) physics.start() physics.setGravity( 0, T ) -- Clear previous scene storyboard.removeAll() W = display.viewableContentWidth H = display.viewableContentHeight GStart = 0 Score = 0 -- local forward references should go here -- -------------------------------------- --HIGH SCORE STUFF-- -------------------------------------- path = system.pathForFile( "Score.txt" ) local scorepath = io.open( path, "r" ) local highest = scorepath:read( "\*a" ) local highscore = tonumber(highest) --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- function gamecreate() local foo = display.newImageRect("foo.png", 150, 150); foo.x = display.contentCenterX foo.y = display.contentCenterY local ground = display.newImageRect("ground.png", display.viewableContentWidth, display.viewableContentHeight \* 0.2); ground.x = display.contentCenterX ground.y = display.viewableContentHeight physics.addBody( foo, "static", {density=0, friction=0.5, bounce=0.5, filter = {maskBits = 4, categoryBits = 2}}) physics.addBody( ground, "static", {density=2, friction=0.5, bounce = 0.6, filter = {maskBits = 4, categoryBits = 2}}) local function drag( event ) if event.phase == "began" then foo.markX = foo.x --Storing X location foo.markY = foo.y --Storing Y location elseif event.phase == "moved" then local x = (event.x - event.xStart) + foo.markX local y = (event.y - event.yStart) + foo.markY foo.x, foo.y = x, y end return true end foo:addEventListener("touch",drag); GStart = GStart + 1 foo.collision = onLocalCollision foo:addEventListener( "collision", j ) end function snowing() meh = math.random( 0, 100 ) if (meh \< 33) then j = display.newImage("bsnow.png"); j.x = math.random( 1, display.viewableContentWidth ) j.y = -50 --j.rotation = math.random( -270, 270 ) physics.addBody( j, { density=0, friction=0.3, bounce=0.4, filter = {maskBits = 2, categoryBits = 4}} ) --, filter = {maskBits = 2, categoryBits = 4} elseif (meh \> 66) then j = display.newImage("gsnow.png"); j.x = math.random( 1, display.viewableContentWidth ) j.y = -50 --j.rotation = math.random( -270, 270 ) physics.addBody( j, { density=0, friction=0.3, bounce=0.2, filter = {maskBits = 2, categoryBits = 4}} ) --, filter = {maskBits = 2, categoryBits = 4} else j = display.newImage("snow.png"); j.x = math.random( 1, display.viewableContentWidth ) j.y = -50 --j.rotation = math.random( -270, 270 ) physics.addBody( j, { density=0, friction=0.2, bounce=0.5, filter = {maskBits = 2, categoryBits = 4}} ) end return lol end function addition() Score = Score + 1 end function onLocalCollision() timer.cancel( snownow ) timer.cancel( count ) local gameover = display.newText("You Lost!", W \* 0.5, H \* 0.5, "Helvetica", 100); gameover.x = W \* 0.5 gameover.y = H \* 0.2 local showmyhigh = display.newText( "Your high score is:", W \* 0.5, H \* 0.7, "Helvetica", 100); showmyhigh.x = W \* 0.5 showmyhigh.y = H \* 0.8 local showmyhigh2 = display.newText( highscore, W \* 0.5, H \* 0.7, "Helvetica", 100); showmyhigh2.x = W \* 0.5 showmyhigh2.y = showmyhigh.y + 20 local showscore = display.newText( Score , W \* 0.5, H \* 0.7, "Helvetica", 100); showscore.x = W \* 0.5 showscore.y = H \* 0.6 local replay = display.newImage( "replay.png" ) replay.x = W \* 0.2 replay.Y = H \* 0.5 local function tapped( event ) storyboard.gotoScene( "scene\_splash" ) end replay:addEventListener( "tap", tapped ) if highscore \< Score then local wow = display.newText( "High Score!", W \* 0.5, H \* 0.7, "Helvetica", 100) wow.x = W \* 0.5 wow.y = H \* 0.4 local path = system.pathForFile( "Score.txt") local file = io.open( path, "w+" ) file:write( Score ) end end -- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view gamecreate() count = timer.performWithDelay( 1000, addition, - 1) if GStart == 1 then snownow = timer.performWithDelay( 300, snowing, -1) end end -- Called BEFORE scene has moved onscreen: function scene:willEnterScene( event ) local group = self.view end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view end -- Called AFTER scene has finished moving offscreen: function scene:didExitScene( event ) local group = self.view end -- Called prior to the removal of scene's "view" (display view) function scene:destroyScene( event ) local group = self.view end -- Called if/when overlay scene is displayed via storyboard.showOverlay() function scene:overlayBegan( event ) local group = self.view local overlay\_name = event.sceneName -- name of the overlay scene end -- Called if/when overlay scene is hidden/removed via storyboard.hideOverlay() function scene:overlayEnded( event ) local group = self.view local overlay\_name = event.sceneName -- name of the overlay scene end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "willEnterScene" event is dispatched before scene transition begins scene:addEventListener( "willEnterScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "didExitScene" event is dispatched after scene has finished transitioning out scene:addEventListener( "didExitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) -- "overlayBegan" event is dispatched when an overlay scene is shown scene:addEventListener( "overlayBegan", scene ) -- "overlayEnded" event is dispatched when an overlay scene is hidden/removed scene:addEventListener( "overlayEnded", scene ) --------------------------------------------------------------------------------- return scene

So in your createScene() in “scene_game.lua”, you call 

gamecreate()

and I don’t see you have inserted your display objects into the scene’s view.

Isn’t it just what I said in my last post?

So should I simply cut all of the functions and turn them into local functions inside the scene’s view, then call them?

When you put:   storyboard:removeAll() at the top of a scene, if that scene is already loaded, it won’t execute again.  Code in the main chunk executes only on initial load.

What I like to do is remove the scene just before I go to it if I need to force a complete reset. 

storyboard.removeScene(“menu”)

storyboard.gotoScene(“menu”)

Keep in mind this is a harsh practice because loading a module and creating your display resources over again is an expensive operation. 

It’s better for you to learn how to properly reset a scene:  

Use createScene() to create objects, but for things that move, don’t expect createScene to be the authority on it. 

Use a willEnterScene() event to reset scene values and move things to their starting positions.

Use enterScene() to start any timers and transitions, any audio that needs to start once the scene is on the screen.  Also create an native.* objects here. 

Use exitScene to undo anything any thing you did in enterScene()

Rarely will you need to do things in didExitScene() or destroyScene(), though if you load any audio in createScene() you will want to dispose it in destoryScene().

There are many ways to do something.

In your gamecreate() function, you can simply add display objects to “scene.view” (which is self.view in createScene()). Or pass self.view to gamecreate() as a parameter so it can be used inside the function.

Or, you can create a display group (& insert into scene’s view) and then all your display objects are inserted into this display group.

There are just too many possibilities. The principle in your case is that you have to insert all graphic objects into the scene’s view no matter “directly” or “indirectly” in order for the storyboard to take care of your display objects.