Once upon a time in a Gideros Q&A forum, someone asked: "Hey, it sucks to put everything on stage, how do you manage scenes in Gideros?". And the answer he got was: "Wait a bit, I'll code something. Here, I've just coded a simple scene manager class". Well, maybe it's a bit exaggerated, but still how's that for support?

And so Scene Manager class appeared. It's usage is simple. In global scope you define class instance with all scenes in it (scene variables and keys to reference them):

--define scenes
sceneManager = SceneManager.new({
	--start scene
	["start"] = start,
	--pack select scene
	["pack_select"] = pack_select,
	--level select scene
	["level_select"] = level_select,
	--level itself
	["level"] = level
})
--add manager to stage
stage:addChild(sceneManager)

Now you can define each scene in it's own file with similar code:

level_select = gideros.class(Sprite)
function level_select:init()
	--do your stuff here
end

And now you can switch between scenes and even use transitions:

--Arguments:
--start - provided key to reference scene
--1 - duration of transition in seconds
--SceneManager.flipWithFade - scene transition
--easing.outBack - easing
sceneManager:changeScene("start", 1, SceneManager.flipWithFade, easing.outBack)

Possible scene transitions:

--Move functions
 SceneManager.moveFromLeft 
 SceneManager.moveFromRight
 SceneManager.moveFromBottom
 SceneManager.moveFromTop
 SceneManager.moveFromLeftWithFade
 SceneManager.moveFromRightWithFade
 SceneManager.moveFromBottomWithFade
 SceneManager.moveFromTopWithFade

--Overlay functions
 SceneManager.overFromLeft
 SceneManager.overFromRight
 SceneManager.overFromBottom
 SceneManager.overFromTop
 SceneManager.overFromLeftWithFade
 SceneManager.overFromRightWithFade
 SceneManager.overFromBottomWithFade
 SceneManager.overFromTopWithFade

--Fade & flip functions
 SceneManager.fade
 SceneManager.crossFade
 SceneManager.flip
 SceneManager.flipWithFade
 SceneManager.flipWithShade

Reseting level can now be as easy as changing to same scene

Couple of things to note:

Do all your stuff inside scene scopes, everything added globally to stage, will always appear there and won't be changed with scenes

And when you add something on enter frame event, it is added globally and you need to remove it when exiting scene, or else it will still be called:

level = gideros.class(Sprite)

function level:init()
	--add on enter frame event
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame)
	--remove event on exiting scene
	self:addEventListener("exitBegin", self.onExitBegin, self)
end

--enter frame event
function level:onEnterFrame() 
	--do something on enter frame
end

--removing event on exiting scene
function level:onExitBegin()
	self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end

Events supported by Scene manager are:

  • enterBegin: Dispatched when your new scene is about to enter the stage. You can initialize your variables here.
  • enterEnd: Dispatched after the transition of new scene has ended. Mostly, you add listeners and start your logic here.
  • exitBegin: Dispatched when your current scene is about to leave the stage. You can remove listeners and stop timers here.
  • exitEnd: Dispatched after your current scene has leaved the stage.

More information about Scene Manager class in Gideros Mobile Blog