Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

function to change scene
Started by novelda76 Dec 12 2018 10:22 AM

5 replies to this topic

Best Answer roaminggamer , 12 December 2018 - 10:49 AM

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.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

novelda76

[GLOBAL: userInfoPane.html]
novelda76
  • Observer

  • 14 posts
  • Corona SDK

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



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,492 posts
  • Corona SDK

  Best Answer

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.



[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,492 posts
  • Corona SDK

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



[TOPIC: post.html]
#4

novelda76

[GLOBAL: userInfoPane.html]
novelda76
  • Observer

  • 14 posts
  • Corona SDK

Thank you very much!!



[TOPIC: post.html]
#5

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,492 posts
  • Corona SDK

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


Edited by roaminggamer, 12 December 2018 - 11:04 AM.


[TOPIC: post.html]
#6

novelda76

[GLOBAL: userInfoPane.html]
novelda76
  • Observer

  • 14 posts
  • Corona SDK

Hi, 

Thanks for the code!!




[topic_controls]
[/topic_controls]