Difference between revisions of "SceneManager"

From GiderosMobile
m (Text replacement - "<source" to "<syntaxhighlight")
Line 11: Line 11:
  
 
Then you need to '''require''' the plugin:
 
Then you need to '''require''' the plugin:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
require "scenemanager"
 
require "scenemanager"
 
</source>
 
</source>
Line 18: Line 18:
 
=== Example ===
 
=== Example ===
 
How to declare and change scenes in your main.lua file
 
How to declare and change scenes in your main.lua file
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
require "scenemanager"
 
require "scenemanager"
 
require "easing"
 
require "easing"
Line 38: Line 38:
  
 
=== The Bare Bone of a Scene Class ===
 
=== The Bare Bone of a Scene Class ===
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
Level01 = Core.class(Sprite)
 
Level01 = Core.class(Sprite)
  
Line 84: Line 84:
  
 
=== A table of all transitions ===
 
=== A table of all transitions ===
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
transitions = {
 
transitions = {
 
SceneManager.moveFromRight, -- 1
 
SceneManager.moveFromRight, -- 1

Revision as of 15:30, 13 July 2023

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.

First you need to add the plugin to your project: right click Plugins -> Add plugin -> SceneManager

Then you need to require the plugin: <syntaxhighlight lang="lua"> require "scenemanager" </source> Note: works in pair with the Easing plugin

Example

How to declare and change scenes in your main.lua file <syntaxhighlight lang="lua"> 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) </source>

Below you will find what a scene class looks like.

The Bare Bone of a Scene Class

<syntaxhighlight 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>

A table of all transitions

<syntaxhighlight lang="lua"> 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 } </source>

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