2015年4月22日 星期三

[Corona SDK] How to load or restart current scene

If we want to change the scene, we can call composer.gotoScene( xxx ).
However, what if we want to reload current scene?
Of course, we can call composer.gotoScene( xxx ), where "xxx" is the name of current scene.

However, you will find that scene:create() is not called.
Only scene:show() is exceuted.
You may move the codes in scene:create() to scene:show() if you want.

Are there any other ways?
We could use storyboard.reloadScene() before.
However, this method is obsolete.
What should we do now?

The simple way is to use a dummy scene.
We can change to the dummy scene and then change back immediately.

local options ={
 effect = "fade",
 time = 0,
 params =
 {
  myData = 1234
 }
}
composer.gotoScene( "RestartDummy", options )
The parameter time can set to be 0 which means that we want to change immediately.
The params are those parameters we want to pass to "RestartDummy".
The codes of "RestartDummy":
local composer = require( "composer" )
local scene = composer.newScene()

function scene:create( event ) 
 print( "((create scene RestartDummy's view))" ) 
 local params = event.params
 print(params.myData)
end --function scene:create( event )

function scene:show( event ) 
 local phase = event.phase 
 if "did" == phase then 
  print( "((show scene RestartDummy's view))" ) 
  composer.removeScene( xxx ) 
  composer.gotoScene( "xxx", "fade", 0 )  
 end 
end

function scene:hide( event ) 
 local phase = event.phase 
 if "will" == phase then 
  print( "((hiding scene RestartDummy's view))" )  
 end 
end

function scene:destroy( event )
 print( "((destroying scene RestartDummy's view))" )
end


-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

return scene

沒有留言:

張貼留言