There are several ways to pass data between scenes. They include:
-
Use the Composer APIs to get the previous scene name:
local prevScene = composer.getSceneName( “previous” ) – Later… composer.gotoScene( prevScene )
-
Using params with the composer.gotoScene() call. With this you could pass in the name of the previous scene.
local params = { currentLevel = 3, gameScene = “level3” } composer.gotoScene( “gameover”, { params = params }
Then in your gameover scene near the top:
local currentLevel local gameScene
Later in your scene:show() event function:
function scene:show( event ) local sceneGroup = self.view local params = event.params if event.phase == "did" then currentLevel = params.currentLevel gameScene = params.gameScene end end
Now when you need to go back to the game level it’s a simple composer.gotoScene( gameScene )
-
Use the composer API’s .setVariable() and .getVariable() to pass the data. In the game level:
composer.setVariable( “gameScene”, “level3” )
In the gameover scene:
local gameScene = composer.getVariable( "gameScene" ) composer.gotoScene( gameScene )
- Use a app wide data module. To learn more about this, please read: http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/
Create a new Lua file called “mydata.lua”
local M = {} return M
In your level file:
local mydata = require( "mydata" ) mydata.gameScene = "level3"
In your gameover scene:
local mydata = require( "mydata" ) scene.gotoScene( mydata.gameScene )
Personally I prefer the last way. I have this data table that I may available in all my scenes. Its like globals with out the problem of using globals. All four are perfectly valid ways of doing what you want to do.
Rob