Reloading a scene using the "cut scene" method results in black screen?

So I’m trying to reload a really simple scene but it always results in a black screen when I do it the second time. 

This how my main.lua looks like

display.setStatusBar( display.HiddenStatusBar ) local composer = require( "composer" ) composer.gotoScene( "test" )

This how test.lua looks like

local composer = require( "composer" ) local scene = composer.newScene() -- "scene:create()" function scene:create( event ) local sceneGroup = self.view -- background local bg = display.newRect(0, 0, display.contentWidth, display.contentHeight) bg.anchorX = 0 bg.anchorY = 0 bg:setFillColor(1) bg:addEventListener( "tap", restart ) sceneGroup:insert( bg) local text = display.newText( "PLAY", display.contentWidth\*0.5, display.contentHeight-200, native.systemFont, 70) text:setFillColor(0) sceneGroup:insert(text) end -- end of scene function restart( event ) composer.gotoScene( "tryagain" ) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

and this is how my tryagain.lua looks like

local composer = require( "composer" ) local scene = composer.newScene() -- "scene:create()" function scene:create( event ) local sceneGroup = self.view composer.removeScene( "test" ) composer.loadScene( "test" ) composer.gotoScene( "test" ) end -- end of scene -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

This is a really basic example that I used to test this issue. So all I’m doing is displaying a white background and every time you tap it it should reload the scene again( display the white background and some text again). When I tap it the first time it seems to work fine. but when I tap it the second  time, I get a black screen and there is no error in the console either. Anyone know why is this happening? Is this a bug? or am i doing something wrong here?

What’s going on in your console log?  Any errors there?

http://docs.coronalabs.com/guide/basics/debugging/index.html

Rob

No. I’m not getting any errors.

You are trying to remove your test scene, load it and go to it while it’s still on the screen.   Here is how things kinda go:

test.lua scene:create() gets called

test.lua scene:show() gets called (will phase)

test.lua transitions on screen

test.lua scene:show() gets called (did phase)

You trigger your restart() function which calls composer.gotoScene(“tryagain”)

                         tryagain.lua:  scene:create() gets called

                         tryagain.lua:  try to remove test.lua, but it’s still on the screen

                         tryagain.lua:  tries to load test.lua but it’s still on the screen

                         tryagain.lua:  tries to goto the scene but it’s still on the screen

                         tryagain.lua:  scene:show (will phase)

test.lua: scene:hide(will phase)

                         tryagain.lua:  scene:show ( did phase)

test.lua: scene:hide( did phase)

So you’ve already tried to destroy and goto the scene that’s on screen, before you ever got the other scene on screen.  This is probably creating an undefined state.

I would move your composer.removeScene() call to your tryagain.lua’s scene:show() in its “did” phase.   Actually create a “try again” scene and let the user interact to go back.

Rob

What’s going on in your console log?  Any errors there?

http://docs.coronalabs.com/guide/basics/debugging/index.html

Rob

No. I’m not getting any errors.

You are trying to remove your test scene, load it and go to it while it’s still on the screen.   Here is how things kinda go:

test.lua scene:create() gets called

test.lua scene:show() gets called (will phase)

test.lua transitions on screen

test.lua scene:show() gets called (did phase)

You trigger your restart() function which calls composer.gotoScene(“tryagain”)

                         tryagain.lua:  scene:create() gets called

                         tryagain.lua:  try to remove test.lua, but it’s still on the screen

                         tryagain.lua:  tries to load test.lua but it’s still on the screen

                         tryagain.lua:  tries to goto the scene but it’s still on the screen

                         tryagain.lua:  scene:show (will phase)

test.lua: scene:hide(will phase)

                         tryagain.lua:  scene:show ( did phase)

test.lua: scene:hide( did phase)

So you’ve already tried to destroy and goto the scene that’s on screen, before you ever got the other scene on screen.  This is probably creating an undefined state.

I would move your composer.removeScene() call to your tryagain.lua’s scene:show() in its “did” phase.   Actually create a “try again” scene and let the user interact to go back.

Rob