Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
How to call sceneManager from sub screen — Gideros Forum

How to call sceneManager from sub screen

edited October 2012 in General questions
Hi all,
I am using SceneManager to change between screen but i do not know how to call sceneManager:changeScene from sub screen. Currently I pass changeScene via userData:
local sceneManager = SceneManager.new({
	["Main"] = Main,
	["MainMenu"] = MainMenu,
	["ChooseYourDifficulty"] = ChooseYourDifficulty,
	["ChooseYourType"] = ChooseYourType,
	["Succeed"] = Succeed,
	["HighScoreScreen"] = HighScoreScreen,
})
 
 
stage:addChild(sceneManager)
 
-- load first scene
 
sceneManager:changeScene("MainMenu", 1, SceneManager.moveFromLeft, easing.outBounce, {userData = {sceneManager=sceneManager}})
And then in MainMenu I get sceneManager back:
function MainMenu:init(params)
	if params then
		sceneManager = params.sceneManager;
	end
 
//.... and then call
	btnFreePlay:addEventListener("click", 
		function() 
			sceneManager:changeScene("ChooseYourDifficulty", 1, SceneManager.moveFromLeft, easing.outBounce, {userData = {sceneManager=sceneManager}})
		end
	)
Is this right approach?

Thanks!

Comments

  • bowerandybowerandy Guru
    edited October 2012 Accepted Answer
    @nextgamevertex, well you could do that but what I do is this...

    I generally have a class that represents my overall game/program, and it is this that holds on to the scene manager. This game class, I make a singleton (i.e. a class that can only have one instance) and I hold onto this instance in a "sharedInstance" field of the class object itself.

    It's probably easier to show you than explain. Here's a bit of a recent demo I wrote that uses scenes. This is the init() for my game class (called BhWaxSceneDemo):
    function BhWaxSceneDemo:init()
    	self.sceneManager=SceneManager.new({
    		["scene1"]=DemoScene1,
    		["scene2"]=DemoScene2,
    		})
     
    	self.sceneManager:changeScene("scene1")
    	stage:addChild(self.sceneManager)
     
    	BhWaxSceneDemo.sharedInstance=self
    end
    Now in my scene classes I can reference the scene manager like this:
    function DemoScene2:onButtonClicked()
    	BhWaxSceneDemo.sharedInstance.sceneManager:changeScene("scene1", 
                1, SceneManager.moveFromLeft)
    end
    I find that in most games, you need to have a central place to store general stuff about the game; high scores, and other global stuff. It seems sensible to keep the scene manager in there too.

    Purists sometimes complain about using the Singleton pattern because it effectively creates a global and globals are frowned upon. However, since we already have each class as a global in Gideros, I feel this is an acceptable and convenient approach.

    BTW, use of the (rather lengthy) name "sharedInstance" is by no means mandatory. I only choose this because that's what people programming in IOS typically use. Another name might be "current".

    best regards
    +1 -1 (+1 / -0 )Share on Facebook
  • zaniarzaniar Member
    edited October 2012
    I always make sceneManager global
    +1 -1 (+1 / -0 )Share on Facebook
  • edited October 2012
    @bowerandy thanks so much! I will try this.

    Edit: Please review this, is this acceptable?

    main.lua
    GameGlobal.new();
    GameGlobal.lua
    GameGlobal = Core.class();
     
    function GameGlobal:init()
    	self.sceneManager = SceneManager.new({
    		["Main"] = Main,
    		["MainMenu"] = MainMenu,
    		["ChooseYourDifficulty"] = ChooseYourDifficulty,
    		["ChooseYourType"] = ChooseYourType,
    		["Succeed"] = Succeed,
    		["HighScoreScreen"] = HighScoreScreen,
    	})
     
    	stage:addChild(self.sceneManager)
    	-- load first scene
    	self.sceneManager:changeScene("MainMenu", 1, SceneManager.moveFromLeft, easing.outBounce)
    	GameGlobal.sharedInstance=self	
    end
    MainMenu.lua
     
            // .....
    	btnFreePlay:addEventListener("click", 
    		function() 
    			GameGlobal.sharedInstance.sceneManager:changeScene("ChooseYourDifficulty", 1, SceneManager.moveFromLeft, easing.outBounce, {userData = {sceneManager=sceneManager}})
    		end
    	)
  • @nextgamevertex, yes that looks how I'd do it.

    best regards
    +1 -1 (+1 / -0 )Share on Facebook
  • Thank you.
    It so helpful.

    Dislikes: roychan

    +1 -1 (+0 / -1 )Share on Facebook
Sign In or Register to comment.