function to change scene

Hello everyone,

I have 10 scenes in my app, each of one is named “scene01”, “scene02”, scene"03"…

I have the same two buttons in every scene to go to the next and previous scene. They are created with widget.newButton()

It’s possible to have a code in a external module with a function to change scenes without having to rewrite the codes for the buttons in every scene?

Thanks in advance

Sure, but that would be a bit of a waste of effort and an over-complication.  i.e. You’d work hard to make it the first time and it could go wrong easily.

The code needed to change scenes is just 1…3 lines depending on how complex the scene transition is and whether you pass any data to the new scene.  Then,  the button listener is a few more lines.

Still if you want to see the basic concept:

  1. Down load this (from my Corona Geek collection): https://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/composer_scene_manager.zip

  2. Unzip it.

  3. Look at folder/project: 16_commonListener

  4. Pay particular attention to these files:

  • scripts/commonListeners.lua
  • ifc/scene*.lua

Note: I use a 30Log based button but the concept is the same for all other button types.

I only duplicated the listeners, but if you wanted you could make  a module (commonButtons.lua for example) using the

same general structure as commonListeners.lua.  This module would have a single method that had the job of:

  1. Providing two dynamically generated listeners.

  2. Producing two buttons using those listeners.

  3. Inserting buttons into sceneGroup you pass in.
     
    i.e.  In scene 1:

    commonButtons.makeButtons( sceneGroup, “scene5”, “scene2” )

 
 
In scene 2:

commonButtons.makeButtons( sceneGroup, "scene1", "scene3" )

In scene 3:

commonButtons.makeButtons( sceneGroup, "scene2", "scene4" )

et cetera…

Thank you very much!!

The module (using by button builder not widget.*) would look like something this:

-- ============================================================= -- Rudimenatary Common Button Builder -- ============================================================= local composer = require( "composer" ) local shared = {} function shared.makeButtons( sceneGroup, d1, d2 ) local function listener1( self, event ) composer.gotoScene( "ifc." .. d1 , { effect = "slideRight", time = 500 } ) end local function listener2( self, event ) composer.gotoScene( "ifc." .. d2 , { effect = "slideLeft", time = 500 } ) end PushButton( sceneGroup, display.contentCenterX - 150, display.contentCenterY, d1, listener1, { labelColor = {0,1,0}, labelSize = 24 } ) PushButton( sceneGroup, display.contentCenterX + 150, display.contentCenterY, d2, listener2, { labelColor = {0,1,1}, labelSize = 24 } ) end return shared

Hi, 

Thanks for the code!!

Sure, but that would be a bit of a waste of effort and an over-complication.  i.e. You’d work hard to make it the first time and it could go wrong easily.

The code needed to change scenes is just 1…3 lines depending on how complex the scene transition is and whether you pass any data to the new scene.  Then,  the button listener is a few more lines.

Still if you want to see the basic concept:

  1. Down load this (from my Corona Geek collection): https://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/composer_scene_manager.zip

  2. Unzip it.

  3. Look at folder/project: 16_commonListener

  4. Pay particular attention to these files:

  • scripts/commonListeners.lua
  • ifc/scene*.lua

Note: I use a 30Log based button but the concept is the same for all other button types.

I only duplicated the listeners, but if you wanted you could make  a module (commonButtons.lua for example) using the

same general structure as commonListeners.lua.  This module would have a single method that had the job of:

  1. Providing two dynamically generated listeners.

  2. Producing two buttons using those listeners.

  3. Inserting buttons into sceneGroup you pass in.
     
    i.e.  In scene 1:

    commonButtons.makeButtons( sceneGroup, “scene5”, “scene2” )

 
 
In scene 2:

commonButtons.makeButtons( sceneGroup, "scene1", "scene3" )

In scene 3:

commonButtons.makeButtons( sceneGroup, "scene2", "scene4" )

et cetera…

Thank you very much!!

The module (using by button builder not widget.*) would look like something this:

-- ============================================================= -- Rudimenatary Common Button Builder -- ============================================================= local composer = require( "composer" ) local shared = {} function shared.makeButtons( sceneGroup, d1, d2 ) local function listener1( self, event ) composer.gotoScene( "ifc." .. d1 , { effect = "slideRight", time = 500 } ) end local function listener2( self, event ) composer.gotoScene( "ifc." .. d2 , { effect = "slideLeft", time = 500 } ) end PushButton( sceneGroup, display.contentCenterX - 150, display.contentCenterY, d1, listener1, { labelColor = {0,1,0}, labelSize = 24 } ) PushButton( sceneGroup, display.contentCenterX + 150, display.contentCenterY, d2, listener2, { labelColor = {0,1,1}, labelSize = 24 } ) end return shared

Hi, 

Thanks for the code!!