Navigation
These archived docs are for Phaser 2.3.0 Phaser 3 docs can be found on newdocs.phaser.io.
Phaser CE docs can be found on the Phaser CE Documentation site.

Constructor

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 code: core/StateManager.js (Line 17)

Public Properties

current : string

The current active State object.

Source code: core/StateManager.js (Line 68)

game : Phaser.Game

A reference to the currently running game.

Source code: core/StateManager.js (Line 22)

onCreateCallback : Function

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

Default Value
  • null
Source code: core/StateManager.js (Line 86)

onInitCallback : Function

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

Default Value
  • null
Source code: core/StateManager.js (Line 74)

onLoadRenderCallback : Function

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

Default Value
  • null
Source code: core/StateManager.js (Line 122)

onLoadUpdateCallback : Function

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

Default Value
  • null
Source code: core/StateManager.js (Line 116)

onPausedCallback : Function

This is called when the game is paused.

Default Value
  • null
Source code: core/StateManager.js (Line 128)

onPauseUpdateCallback : Function

This is called every frame while the game is paused.

Default Value
  • null
Source code: core/StateManager.js (Line 140)

onPreloadCallback : Function

This is called when the state starts to load assets.

Default Value
  • null
Source code: core/StateManager.js (Line 80)

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.

Default Value
  • null
Source code: core/StateManager.js (Line 110)

onRenderCallback : Function

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

Default Value
  • null
Source code: core/StateManager.js (Line 98)

onResizeCallback : Function

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

Default Value
  • null
Source code: core/StateManager.js (Line 104)

onResumedCallback : Function

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

Default Value
  • null
Source code: core/StateManager.js (Line 134)

onShutDownCallback : Function

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

Default Value
  • null
Source code: core/StateManager.js (Line 146)

onUpdateCallback : Function

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

Default Value
  • null
Source code: core/StateManager.js (Line 92)

states : Object

The object containing Phaser.States.

Source code: core/StateManager.js (Line 27)

Public 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 either a Phaser.State object (or an object that extends it), a plain JavaScript object or a function. If a function is given a new state object will be created by calling it.

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 code: core/StateManager.js (Line 169)

checkState(key) → {boolean}

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.

Returns
boolean -

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

Source code: core/StateManager.js (Line 405)

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 code: core/StateManager.js (Line 360)

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 code: core/StateManager.js (Line 708)

getCurrentState()

Gets the current State.

Returns

Phaser.State

Source code: core/StateManager.js (Line 556)

Links game properties to the State given by the key.

Parameters
Name Type Description
key string

State key.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: core/StateManager.js (Line 439)

<internal> loadComplete()

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: core/StateManager.js (Line 567)

<internal> pause()

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: core/StateManager.js (Line 585)

<internal> pauseUpdate()

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: core/StateManager.js (Line 631)

<internal> preRender(elapsedTime)

Parameters
Name Type Description
elapsedTime number

The time elapsed since the last update.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: core/StateManager.js (Line 651)

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 code: core/StateManager.js (Line 310)

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 code: core/StateManager.js (Line 217)

<internal> render()

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: core/StateManager.js (Line 678)

<internal> resize()

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: core/StateManager.js (Line 665)

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 code: core/StateManager.js (Line 277)

<internal> resume()

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: core/StateManager.js (Line 598)

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 code: core/StateManager.js (Line 248)

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

Parameters
Name Type Description
key string

State key.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: core/StateManager.js (Line 470)

<internal> update()

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: core/StateManager.js (Line 611)