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
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):
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
Likes: nextgamevertex
Likes: nextgamevertex
Edit: Please review this, is this acceptable?
main.lua
best regards
Likes: nextgamevertex
It so helpful.
Dislikes: roychan