Difference between revisions of "SceneManager"

From GiderosMobile
m
(added some more explanations)
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/>
'''<translate>Inherits from</translate>:''' [[Special:MyLanguage/Sprite|Sprite]]<br/>
+
'''Inherits from:''' [[Special:MyLanguage/Sprite|Sprite]]<br/>
  
 
=== Description ===
 
=== Description ===
An easy way to manage scenes.<br/>
+
An easy way to manage scenes.
'''Note''': works in pair with the [[Easing]] plugin
 
 
<source lang="lua">
 
<source lang="lua">
 
require "scenemanager"
 
require "scenemanager"
 
</source>
 
</source>
 +
'''Note''': works in pair with the [[Easing]] plugin
  
 
=== Example ===
 
=== Example ===
Line 21: Line 21:
 
{
 
{
 
["menu"] = Menu,
 
["menu"] = Menu,
["level1"] = Level1,
+
["level01"] = Level01,
 
}
 
}
 
)
 
)
Line 27: Line 27:
 
-- from app start to menu
 
-- from app start to menu
 
scenemanager:changeScene("menu")
 
scenemanager:changeScene("menu")
-- from menu to level1
+
-- from menu to level01
--scenemanager:changeScene("level1", 1, SceneManager.moveFromRight, easing.outBack)
+
--scenemanager:changeScene("level01", 1, SceneManager.moveFromRight, easing.outBack)
 +
</source>
 +
 
 +
Below you will find what a scene class looks like.
 +
 
 +
=== The Bare Bone of a Scene Class ===
 +
<source lang="lua">
 +
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
 
</source>
 
</source>
  
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Methods</translate> ===
+
=== Methods ===
[[Special:MyLanguage/SceneManager.new|SceneManager.new]] ''<translate>creates a new SceneManager object</translate>''<br/><!-- GIDEROSMTD:SceneManager.new(table) creates a new SceneManager object -->
+
[[Special:MyLanguage/SceneManager.new|SceneManager.new]] ''creates a new SceneManager object''<br/><!-- GIDEROSMTD:SceneManager.new(table) creates a new SceneManager object -->
  
[[Special:MyLanguage/SceneManager:changeScene|SceneManager:changeScene]] ''<translate>transitions to a scene</translate>''<br/><!-- GIDEROSMTD:SceneManager:changeScene(scene, duration, transition, ease, options) transitions to a scene -->
+
[[Special:MyLanguage/SceneManager:changeScene|SceneManager:changeScene]] ''transitions to a scene''<br/><!-- GIDEROSMTD:SceneManager:changeScene(scene, duration, transition, ease, options) transitions to a scene -->
  
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Events</translate> ===
+
=== Events ===
 
[[Special:MyLanguage/SceneManager_enterBegin|"enterBegin"]]<br/><!-- GIDEROSEVT:enterBegin-->
 
[[Special:MyLanguage/SceneManager_enterBegin|"enterBegin"]]<br/><!-- GIDEROSEVT:enterBegin-->
 
[[Special:MyLanguage/SceneManager_enterEnd|"enterEnd"]]<br/><!-- GIDEROSEVT:enterEnd-->
 
[[Special:MyLanguage/SceneManager_enterEnd|"enterEnd"]]<br/><!-- GIDEROSEVT:enterEnd-->
 
[[Special:MyLanguage/SceneManager_exitBegin|"exitBegin"]]<br/><!-- GIDEROSEVT:exitBegin-->
 
[[Special:MyLanguage/SceneManager_exitBegin|"exitBegin"]]<br/><!-- GIDEROSEVT:exitBegin-->
 
[[Special:MyLanguage/SceneManager_exitEnd|"exitEnd"]]<br/><!-- GIDEROSEVT:exitEnd-->
 
[[Special:MyLanguage/SceneManager_exitEnd|"exitEnd"]]<br/><!-- GIDEROSEVT:exitEnd-->
=== <translate>Constants</translate> ===
+
=== Constants ===
 
SceneManager.moveFromRight<br/><!-- GIDEROSCST:SceneManager.moveFromRight-->
 
SceneManager.moveFromRight<br/><!-- GIDEROSCST:SceneManager.moveFromRight-->
 
SceneManager.moveFromLeft<br/><!-- GIDEROSCST:SceneManager.moveFromLeft-->
 
SceneManager.moveFromLeft<br/><!-- GIDEROSCST:SceneManager.moveFromLeft-->

Revision as of 04:30, 5 November 2020

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