how to go to previous scene

Hello people,

My name is Joshua and I’m stuck with quite a problem for my new android game app.

I know how to switch to specific scenes, but I don’t know how to go to the previous scene.

I have this game and whenever you are gameover you will be sent to a “gameover” scene, however in the “gameover” scene there are 2 buttons, a “back to menu” button and a “Try again” button.

On the try agin button I would like to sent the player to the previous scene, that includes the level the player was.

I could solve this problem by copying and pasting the “gameover” scene over and over after each level, but this wil consume a lot of data!

I hope you guys can help me with this problem.

Thanks in advance!

There are several ways to pass data between scenes. They include:

  1. Use the Composer APIs to get the previous scene name:

    local prevScene = composer.getSceneName( “previous” ) – Later… composer.gotoScene( prevScene )

  2. 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 )

  1. 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 )
  1. 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

There are several ways to pass data between scenes. They include:

  1. Use the Composer APIs to get the previous scene name:

    local prevScene = composer.getSceneName( “previous” ) – Later… composer.gotoScene( prevScene )

  2. 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 )

  1. 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 )
  1. 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