Class: StateManager

Phaser. StateManager

new StateManager(game [, pendingState])

The State Manager is responsible for loading, setting up and switching game states.

Parameters:
Name Type Argument Default Description
game Phaser.Game

A reference to the currently running game.

pendingState Phaser.State | Object <optional>
null

A State object to seed the manager with.

Source:
src/core/StateManager.js line 17

Members

<readonly> created : boolean

True if the current state has had its create method run (if it has one, if not this is true by default).

Type:
  • boolean
Source:
src/core/StateManager.js line 816

current : string

The current active State object.

Type:
  • string
Source:
src/core/StateManager.js line 69

game : Phaser.Game

A reference to the currently running game.

Type:
Source:
src/core/StateManager.js line 23

onCreateCallback : function

This is called when the state preload has finished and creation begins.

Type:
  • function
Source:
src/core/StateManager.js line 101

onInitCallback : function

This is called when the state is set as the active state.

Type:
  • function
Source:
src/core/StateManager.js line 89

onLoadRenderCallback : function

This is called when the State is rendered during the preload phase.

Type:
  • function
Source:
src/core/StateManager.js line 137

onLoadUpdateCallback : function

This is called when the State is updated during the preload phase.

Type:
  • function
Source:
src/core/StateManager.js line 131

onPausedCallback : function

This is called when the game is paused.

Type:
  • function
Source:
src/core/StateManager.js line 143

onPauseUpdateCallback : function

This is called every frame while the game is paused.

Type:
  • function
Source:
src/core/StateManager.js line 155

onPreloadCallback : function

This is called when the state starts to load assets.

Type:
  • function
Source:
src/core/StateManager.js line 95

onPreRenderCallback : function

This is called before the state is rendered and before the stage is cleared but after all game objects have had their final properties adjusted.

Type:
  • function
Source:
src/core/StateManager.js line 125

onRenderCallback : function

This is called post-render. It doesn't happen during preload (see onLoadRenderCallback).

Type:
  • function
Source:
src/core/StateManager.js line 113

onResizeCallback : function

This is called if ScaleManager.scalemode is RESIZE and a resize event occurs. It's passed the new width and height.

Type:
  • function
Source:
src/core/StateManager.js line 119

onResumedCallback : function

This is called when the game is resumed from a paused state.

Type:
  • function
Source:
src/core/StateManager.js line 149

onShutDownCallback : function

This is called when the state is shut down (i.e. swapped to another state).

Type:
  • function
Source:
src/core/StateManager.js line 161

onStateChange : Phaser.Signal

onStateChange is a Phaser.Signal that is dispatched whenever the game changes state.

It is dispatched only when the new state is started, which isn't usually at the same time as StateManager.start is called because state swapping is done in sync with the game loop. It is dispatched before any of the new states methods (such as preload and create) are called, and after the previous states shutdown method has been run.

The callback you specify is sent two parameters: the string based key of the new state, and the second parameter is the string based key of the old / previous state.

Type:
Source:
src/core/StateManager.js line 83

onUpdateCallback : function

This is called when the state is updated, every game loop. It doesn't happen during preload (@see onLoadUpdateCallback).

Type:
  • function
Source:
src/core/StateManager.js line 107

states : object

The object containing Phaser.States.

Type:
  • object
Source:
src/core/StateManager.js line 28

Methods

add(key, state [, autoStart])

Adds a new State into the StateManager. You must give each State a unique key by which you'll identify it.

The State can be any of

  • a plain JavaScript object containing at least one required callback (see checkState)
  • an instance of Phaser.State
  • an instance of a class extending Phaser.State
  • a constructor function (class)

If a function is given a new state object will be created by calling it, passing the current game as the first argument.

Parameters:
Name Type Argument Default Description
key string

A unique key you use to reference this state, i.e. "MainMenu", "Level1".

state Phaser.State | object | function

The state you want to switch to.

autoStart boolean <optional>
false

If true the State will be started immediately after adding it.

Source:
src/core/StateManager.js line 185

checkState(key)

Checks if a given phaser state is valid. A State is considered valid if it has at least one of the core functions: preload, create, update or render.

Parameters:
Name Type Description
key string

The key of the state you want to check.

Source:
src/core/StateManager.js line 439
Returns:

true if the State has the required functions, otherwise false.

Type
boolean

clearCurrentState()

This method clears the current State, calling its shutdown callback. The process also removes any active tweens, resets the camera, resets input, clears physics, removes timers and if set clears the world and cache too.

Source:
src/core/StateManager.js line 393

destroy()

Removes all StateManager callback references to the State object, nulls the game reference and clears the States object. You don't recover from this without rebuilding the Phaser instance again.

Source:
src/core/StateManager.js line 777

getCurrentState()

Gets the current State.

Source:
src/core/StateManager.js line 597
Returns:
Type
Phaser.State

Links game properties to the State given by the key.

Parameters:
Name Type Description
key string

State key.

Source:
src/core/StateManager.js line 471

<protected> loadComplete()

Source:
src/core/StateManager.js line 609
See:

<protected> loadUpdate()

Source:
src/core/StateManager.js line 629
See:
  • Phaser.Loader#finishedLoading

<protected> pause()

Source:
src/core/StateManager.js line 644

<protected> pauseUpdate()

Source:
src/core/StateManager.js line 694

<protected> preRender(elapsedTime)

Parameters:
Name Type Description
elapsedTime number

The time elapsed since the last update.

Source:
src/core/StateManager.js line 716

preUpdate()

preUpdate is called right at the start of the game loop. It is responsible for changing to a new state that was requested previously.

Source:
src/core/StateManager.js line 338

remove(key)

Delete the given state.

Parameters:
Name Type Description
key string

A unique key you use to reference this state, i.e. "MainMenu", "Level1".

Source:
src/core/StateManager.js line 241

<protected> render()

Source:
src/core/StateManager.js line 745

<protected> resize()

Source:
src/core/StateManager.js line 731

restart( [clearWorld] [, clearCache], parameter)

Restarts the current State. State.shutDown will be called (if it exists) before the State is restarted.

Parameters:
Name Type Argument Default Description
clearWorld boolean <optional>
true

Clear everything in the world? This clears the World display list fully (but not the Stage, so if you've added your own objects to the Stage they will need managing directly)

clearCache boolean <optional>
false

Clear the Game.Cache? This purges out all loaded assets. The default is false and you must have clearWorld=true if you want to clearCache as well.

parameter * <repeatable>

Additional parameters that will be passed to the State.init function if it has one.

Source:
src/core/StateManager.js line 303

<protected> resume()

Source:
src/core/StateManager.js line 658

start(key [, clearWorld] [, clearCache], parameter)

Start the given State. If a State is already running then State.shutDown will be called (if it exists) before switching to the new State.

Parameters:
Name Type Argument Default Description
key string

The key of the state you want to start.

clearWorld boolean <optional>
true

Clear everything in the world? This clears the World display list fully (but not the Stage, so if you've added your own objects to the Stage they will need managing directly)

clearCache boolean <optional>
false

Clear the Game.Cache? This purges out all loaded assets. The default is false and you must have clearWorld=true if you want to clearCache as well.

parameter * <repeatable>

Additional parameters that will be passed to the State.init function (if it has one).

Source:
src/core/StateManager.js line 273

Nulls all State level Phaser properties, including a reference to Game.

Parameters:
Name Type Description
key string

State key.

Source:
src/core/StateManager.js line 505

<protected> update()

Source:
src/core/StateManager.js line 672

phaser-ce@2.10.6 is on GitHub and NPM

Documentation generated by JSDoc 3.5.4 on 2018-06-01 using Tomorrow.