Difference between revisions of "SceneManager"

From GiderosMobile
m (added one more example)
m (added disclaimer about adding a different scene between the reloading of the same scene.)
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
<!-- GIDEROSOBJ:SceneManager -->
 
<!-- GIDEROSOBJ:SceneManager -->
'''<translate>Supported platforms</translate>:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform pc.png]][[File:Platform mac.png]][[File:Platform winrt.png]][[File:Platform win32.png]]<br/>
+
'''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform pc.png]][[File:Platform mac.png]][[File:Platform winrt.png]][[File:Platform win32.png]]<br/>
'''<translate>Available since</translate>:''' 2020.2<br/>
+
'''Available since:''' 2020.2<br/>
 +
'''Inherits from:''' [[Sprite]]<br/>
  
 
=== Description ===
 
=== Description ===
An easy way to manage scenes.
+
An easy way to manage scenes with transitions.
<source lang="lua">
+
 
 +
'''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:
 +
<syntaxhighlight lang="lua">
 
require "scenemanager"
 
require "scenemanager"
</source>
+
require "easing"
 +
</syntaxhighlight>
  
=== Example ===
+
=== Declaring and Changing Scenes ===
A table holding all scene transitions
+
In your '''main.lua''' file
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
require "scenemanager"
 
require "scenemanager"
-- TRANSITIONS
+
require "easing"
 +
 
 +
scenemanager = SceneManager.new(
 +
{
 +
["menu"] = Menu,
 +
["level01"] = Level01,
 +
-- ...
 +
}
 +
)
 +
stage:addChild(scenemanager)
 +
-- from app start to menu
 +
scenemanager:changeScene("menu")
 +
</syntaxhighlight>
 +
 
 +
The scene manager is set up.
 +
 
 +
'''Please be aware''' that you should not switch scene just by reloading the same scene with different variable values, or Gideros might not be able to properly clean up the referenced stuff. '''Always put another scene in between''' (even a blank one should work: see https://forum.gideros.rocks/discussion/comment/68994/#Comment_68994 )
 +
 
 +
=== 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):
 +
<syntaxhighlight lang="lua">
 +
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
 +
</syntaxhighlight>
 +
 
 +
=== 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:
 +
<syntaxhighlight lang="lua">
 
transitions = {
 
transitions = {
 
SceneManager.moveFromRight, -- 1
 
SceneManager.moveFromRight, -- 1
Line 38: Line 115:
 
SceneManager.flipWithShade, -- 21
 
SceneManager.flipWithShade, -- 21
 
}
 
}
</source>
+
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
 +
}
 +
</syntaxhighlight>
  
How to declare and change scenes
+
=== See also ===
<source lang="lua">
+
'''[[UI_Buttons]]'''
require "scenemanager"
 
require "easing"
 
  
scenemanager = SceneManager.new(
+
{|-
{
+
| style="width: 50%; vertical-align:top;"|
["menu"] = Menu,
+
=== Methods ===
["level1"] = Level1,
+
[[SceneManager.new]] ''creates a new SceneManager object''<br/><!-- GIDEROSMTD:SceneManager.new(table) creates a new SceneManager object -->
}
 
)
 
  
stage:addChild(scenemanager)
+
[[SceneManager:changeScene]] ''transitions to a scene''<br/><!-- GIDEROSMTD:SceneManager:changeScene(scene, duration, transition, ease, options) transitions to a scene -->
scenemanager:changeScene("menu")
 
--scenemanager:changeScene("level1", 1, SceneManager.moveFromRight, easing.outBack)
 
</source>
 
  
{|-
 
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Methods</translate> ===
+
=== Events ===
| style="width: 50%; vertical-align:top;"|
+
[[SceneManager_enterBegin|"enterBegin"]] ''Scene is being entered'' <br/><!--GIDEROSEVT:enterBegin Scene is being entered-->
=== <translate>Events</translate> ===
+
[[SceneManager_enterEnd|"enterEnd"]] ''Scene has been entered'' <br/><!--GIDEROSEVT:enterEnd Scene has been entered-->
=== <translate>Constants</translate> ===
+
[[SceneManager_exitBegin|"exitBegin"]] ''Scene is being exited''<br/><!--GIDEROSEVT:exitBegin Scene is being exited-->
 +
[[SceneManager_exitEnd|"exitEnd"]] ''Scene has been exited'' <br/><!--GIDEROSEVT:exitEnd Scene has been exited-->
 +
 
 +
=== Constants ===
 +
SceneManager.moveFromRight ''Move from the right'' <br/><!--GIDEROSCST:SceneManager.moveFromRight Move from the right-->
 +
SceneManager.moveFromLeft ''Move from the left'' <br/><!--GIDEROSCST:SceneManager.moveFromLeft Move from the left-->
 +
SceneManager.moveFromBottom ''Move from the bottom'' <br/><!--GIDEROSCST:SceneManager.moveFromBottom Move from the bottom-->
 +
SceneManager.moveFromTop ''Move from the top'' <br/><!--GIDEROSCST:SceneManager.moveFromTop Move from the top-->
 +
SceneManager.moveFromRightWithFade ''Move from the right with fading'' <br/><!--GIDEROSCST:SceneManager.moveFromRightWithFade Move from the right with fading-->
 +
SceneManager.moveFromLeftWithFade ''Move from the left with fading'' <br/><!--GIDEROSCST:SceneManager.moveFromLeftWithFade Move from the left with fading-->
 +
SceneManager.moveFromBottomWithFade ''Move from the bottom with fading'' <br/><!--GIDEROSCST:SceneManager.moveFromBottomWithFade Move from the bottom with fading-->
 +
SceneManager.moveFromTopWithFade ''Move from the top with fading'' <br/><!--GIDEROSCST:SceneManager.moveFromTopWithFade Move from the top with fading-->
 +
SceneManager.overFromRight ''Slide over from the right'' <br/><!--GIDEROSCST:SceneManager.overFromRight Slide over from the right-->
 +
SceneManager.overFromLeft ''Slide over from the left'' <br/><!--GIDEROSCST:SceneManager.overFromLeft Slide over from the left-->
 +
SceneManager.overFromBottom ''Slide over from the bottom'' <br/><!--GIDEROSCST:SceneManager.overFromBottom Slide over from the bottom-->
 +
SceneManager.overFromTop ''Slide over from the top'' <br/><!--GIDEROSCST:SceneManager.overFromTop Slide over from the top-->
 +
SceneManager.overFromRightWithFade ''Slide over from the right with fading'' <br/><!--GIDEROSCST:SceneManager.overFromRightWithFade Slide over from the right with fading-->
 +
SceneManager.overFromLeftWithFade ''Slide over from the left with fading'' <br/><!--GIDEROSCST:SceneManager.overFromLeftWithFade Slide over from the left with fading-->
 +
SceneManager.overFromBottomWithFade ''Slide over from the bottom with fading'' <br/><!--GIDEROSCST:SceneManager.overFromBottomWithFade Slide over from the bottom with fading-->
 +
SceneManager.overFromTopWithFade ''Slide over from the top with fading'' <br/><!--GIDEROSCST:SceneManager.overFromTopWithFade Slide over from the top with fading-->
 +
SceneManager.fade ''Fade over'' <br/><!--GIDEROSCST:SceneManager.fade Fade over-->
 +
SceneManager.crossFade ''Cross fade'' <br/><!--GIDEROSCST:SceneManager.crossFade Cross fade-->
 +
SceneManager.flip ''Flip'' <br/><!--GIDEROSCST:SceneManager.flip Flip-->
 +
SceneManager.flipWithFade ''Flip with fading'' <br/><!--GIDEROSCST:SceneManager.flipWithFade Flip with fading-->
 +
SceneManager.flipWithShade ''Flip with shading'' <br/><!--GIDEROSCST:SceneManager.flipWithShade Flip with shading-->
 
|}
 
|}
  
 
{{GIDEROS IMPORTANT LINKS}}
 
{{GIDEROS IMPORTANT LINKS}}

Latest revision as of 22:25, 19 June 2024

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.

Please be aware that you should not switch scene just by reloading the same scene with different variable values, or Gideros might not be able to properly clean up the referenced stuff. Always put another scene in between (even a blank one should work: see https://forum.gideros.rocks/discussion/comment/68994/#Comment_68994 )

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