SceneManager

From GiderosMobile
Revision as of 04:30, 5 November 2020 by MoKaLux (talk | contribs) (added some more explanations)

Supported platforms: Platform android.pngPlatform ios.pngPlatform pc.pngPlatform mac.pngPlatform winrt.pngPlatform win32.png
Available since: 2020.2
Inherits from: Sprite

Description

An easy way to manage scenes.

require "scenemanager"

Note: works in pair with the Easing plugin

Example

How to declare and change scenes

require "scenemanager"
require "easing"

scenemanager = SceneManager.new(
	{
		["menu"] = Menu,
		["level01"] = Level01,
	}
)
stage:addChild(scenemanager)
-- from app start to menu
scenemanager:changeScene("menu")
-- from menu to level01
--scenemanager:changeScene("level01", 1, SceneManager.moveFromRight, easing.outBack)

Below you will find what a scene class looks like.

The Bare Bone of a Scene Class

Level01 = Core.class(Sprite)

function Level01:init()
	-- BG
	application:setBackgroundColor(0x1234AA)

	-- LISTENERS
	self:addEventListener("enterBegin", self.onTransitionInBegin, self)
	self:addEventListener("enterEnd", self.onTransitionInEnd, self)
	self:addEventListener("exitBegin", self.onTransitionOutBegin, self)
	self:addEventListener("exitEnd", self.onTransitionOutEnd, self)
end

-- GAME LOOP
function Level01:onEnterFrame(e)
end

-- EVENT LISTENERS
function Level01:onTransitionInBegin()
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end

function Level01:onTransitionInEnd()
	self:myKeysPressed()
end

function Level01:onTransitionOutBegin()
	self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end

function Level01:onTransitionOutEnd()
end

-- KEYS HANDLER
function Level01:myKeysPressed()
	self:addEventListener(Event.KEY_DOWN, function(e)
		-- for mobiles and desktops
		if e.keyCode == KeyCode.BACK or e.keyCode == KeyCode.ESC then
			scenemanager:changeScene("menu", 1, transitions[2], easing.outBack)
		end
	end)
end

Methods

SceneManager.new creates a new SceneManager object

SceneManager:changeScene transitions to a scene

Events

"enterBegin"
"enterEnd"
"exitBegin"
"exitEnd"

Constants

SceneManager.moveFromRight
SceneManager.moveFromLeft
SceneManager.moveFromBottom
SceneManager.moveFromTop
SceneManager.moveFromRightWithFade
SceneManager.moveFromLeftWithFade
SceneManager.moveFromBottomWithFade
SceneManager.moveFromTopWithFade
SceneManager.overFromRight
SceneManager.overFromLeft
SceneManager.overFromBottom
SceneManager.overFromTop
SceneManager.overFromRightWithFade
SceneManager.overFromLeftWithFade
SceneManager.overFromBottomWithFade
SceneManager.overFromTopWithFade
SceneManager.fade
SceneManager.crossFade
SceneManager.flip
SceneManager.flipWithFade
SceneManager.flipWithShade