SceneManager

From GiderosMobile
(Redirected from Category:SceneManager)

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

Note: works in pair with the Easing plugin


First you need to add the plugins to your project: right click Plugins -> Add plugin -> SceneManager (please do the same for the Easing plugin).

Then you need to require the plugins:

require "scenemanager"
require "easing"

Declaring and Changing Scenes

In your main.lua file

require "scenemanager"
require "easing"

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

The scene manager is set up.

A Scene Class

You can create all your scenes as you please. For example, you can create a folder called "scenes" and create a file for each scenes ("menu.lua", "level01.lua", ...).

Below is what a scene class may look like (feel free to copy paste it):

Menu = Core.class(Sprite)

function Menu: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 Menu:onEnterFrame(e)
end

-- event listeners
function Menu:onTransitionInBegin()
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end
function Menu:onTransitionInEnd()
	self:myKeysPressed()
end
function Menu:onTransitionOutBegin()
	self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end
function Menu:onTransitionOutEnd()
end

-- keys handler
function Menu:myKeysPressed()
	self:addEventListener(Event.KEY_DOWN, function(e)
		if e.keyCode == KeyCode.BACK or e.keyCode == KeyCode.ESC then -- for mobiles and desktops
			application:exit()
		elseif e.keyCode == KeyCode.ENTER then
			scenemanager:changeScene("level01", 1, SceneManager.moveFromLeft, easing.outBack)
--			scenemanager:changeScene("level01", 1, transitions[2], easings[2]) -- nicer
		end
	end)
end

Transitions and Easings table (optional)

A nicer way to apply effects for scene transitions is to store them in tables. In your main.lua you can add at the bottom those tables:

transitions = {
	SceneManager.moveFromRight, -- 1
	SceneManager.moveFromLeft, -- 2
	SceneManager.moveFromBottom, -- 3
	SceneManager.moveFromTop, -- 4
	SceneManager.moveFromRightWithFade, -- 5
	SceneManager.moveFromLeftWithFade, -- 6
	SceneManager.moveFromBottomWithFade, -- 7
	SceneManager.moveFromTopWithFade, -- 8
	SceneManager.overFromRight, -- 9
	SceneManager.overFromLeft, -- 10
	SceneManager.overFromBottom, -- 11
	SceneManager.overFromTop, -- 12
	SceneManager.overFromRightWithFade, -- 13
	SceneManager.overFromLeftWithFade, -- 14
	SceneManager.overFromBottomWithFade, -- 15
	SceneManager.overFromTopWithFade, -- 16
	SceneManager.fade, -- 17
	SceneManager.crossFade, -- 18
	SceneManager.flip, -- 19
	SceneManager.flipWithFade, -- 20
	SceneManager.flipWithShade, -- 21
}
easings = {
	easing.inBack, -- 1
	easing.outBack, -- 2
	easing.inOutBack, -- 3
	easing.inBounce, -- 4
	easing.outBounce, -- 5
	easing.inOutBounce, -- 6
	easing.inCircular, -- 7
	easing.outCircular, -- 8
	easing.inOutCircular, -- 9
	easing.inCubic, -- 10
	easing.outCubic, -- 11
	easing.inOutCubic, -- 12
	easing.inElastic, -- 13
	easing.outElastic, -- 14
	easing.inOutElastic, -- 15
	easing.inExponential, -- 16
	easing.outExponential, -- 17
	easing.inOutExponential, -- 18
	easing.linear, -- 19
	easing.inQuadratic, -- 20
	easing.outQuadratic, -- 21
	easing.inOutQuadratic, -- 22
	easing.inQuartic, -- 23
	easing.outQuartic, -- 24
	easing.inOutQuartic, -- 25
	easing.inQuintic, -- 26
	easing.outQuintic, -- 27
	easing.inOutQuintic, -- 28
	easing.inSine, -- 29
	easing.outSine, -- 30
	easing.inOutSine, -- 31
}

See also

UI_Buttons

Methods

SceneManager.new creates a new SceneManager object

SceneManager:changeScene transitions to a scene

Events

"enterBegin" Scene is being entered
"enterEnd" Scene has been entered
"exitBegin" Scene is being exited
"exitEnd" Scene has been exited

Constants

SceneManager.moveFromRight Move from the right
SceneManager.moveFromLeft Move from the left
SceneManager.moveFromBottom Move from the bottom
SceneManager.moveFromTop Move from the top
SceneManager.moveFromRightWithFade Move from the right with fading
SceneManager.moveFromLeftWithFade Move from the left with fading
SceneManager.moveFromBottomWithFade Move from the bottom with fading
SceneManager.moveFromTopWithFade Move from the top with fading
SceneManager.overFromRight Slide over from the right
SceneManager.overFromLeft Slide over from the left
SceneManager.overFromBottom Slide over from the bottom
SceneManager.overFromTop Slide over from the top
SceneManager.overFromRightWithFade Slide over from the right with fading
SceneManager.overFromLeftWithFade Slide over from the left with fading
SceneManager.overFromBottomWithFade Slide over from the bottom with fading
SceneManager.overFromTopWithFade Slide over from the top with fading
SceneManager.fade Fade over
SceneManager.crossFade Cross fade
SceneManager.flip Flip
SceneManager.flipWithFade Flip with fading
SceneManager.flipWithShade Flip with shading