# Class: StateManager

## Phaser. 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:
    - core/StateManager.js, line 17

### Members

#### current

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `current` | string | The current active State object (defaults to null).

  Source:
    - core/StateManager.js, line 67

#### game

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `game` | Phaser.Game | A reference to the currently running game.

  Source:
    - core/StateManager.js, line 22

#### onCreateCallback

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onCreateCallback` | function | This will be called when create states (setup states...).

  Source:
    - core/StateManager.js, line 82

#### onInitCallback

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onInitCallback` | function | This will be called when the state is started (i.e. set as the current active state).

  Source:
    - core/StateManager.js, line 72

#### onLoadRenderCallback

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onLoadRenderCallback` | function | This will be called when the State is rendered but only during the load process.

  Source:
    - core/StateManager.js, line 107

#### onLoadUpdateCallback

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onLoadUpdateCallback` | function | This will be called when the State is updated but only during the load process.

  Source:
    - core/StateManager.js, line 102

#### onPausedCallback

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onPausedCallback` | function | This will be called once each time the game is paused.

  Source:
    - core/StateManager.js, line 112

#### onPauseUpdateCallback

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onPauseUpdateCallback` | function | This will be called every frame while the game is paused.

  Source:
    - core/StateManager.js, line 122

#### onPreloadCallback

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onPreloadCallback` | function | This will be called when init states (loading assets...).

  Source:
    - core/StateManager.js, line 77

#### onPreRenderCallback

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onPreRenderCallback` | function | This will be called before the State is rendered and before the stage is cleared.

  Source:
    - core/StateManager.js, line 97

#### onRenderCallback

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onRenderCallback` | function | This will be called when the State is rendered, this doesn't happen during load (see onLoadRenderCallback).

  Source:
    - core/StateManager.js, line 92

#### onResumedCallback

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onResumedCallback` | function | This will be called once each time the game is resumed from a paused state.

  Source:
    - core/StateManager.js, line 117

#### onShutDownCallback

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onShutDownCallback` | function | This will be called when the state is shut down (i.e. swapped to another state).

  Source:
    - core/StateManager.js, line 127

#### onUpdateCallback

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onUpdateCallback` | function | This will be called when State is updated, this doesn't happen during load (@see onLoadUpdateCallback).

  Source:
    - core/StateManager.js, line 87

#### states

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `states` | Object | The object containing Phaser.States.

  Source:
    - core/StateManager.js, line 27

### 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:
    - core/StateManager.js, line 159

#### 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.

  Source:
    - core/StateManager.js, line 365

  ##### Returns:

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

  Type
    boolean

#### 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:
    - core/StateManager.js, line 607

#### getCurrentState()

  Gets the current State.

  Source:
    - core/StateManager.js, line 468

  ##### Returns:

  Phaser.State

#### <protected> link(key)

  Links game properties to the State given by the key.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | State key.

  Source:
    - core/StateManager.js, line 399

#### <protected> loadComplete()

  Source:
    - core/StateManager.js, line 479

#### <protected> pause()

  Source:
    - core/StateManager.js, line 497

#### <protected> pauseUpdate()

  Source:
    - core/StateManager.js, line 543

#### <protected> preRender()

  Source:
    - core/StateManager.js, line 563

#### 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:
    - core/StateManager.js, line 298

#### 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:
    - core/StateManager.js, line 207

#### <protected> render()

  Source:
    - core/StateManager.js, line 576

#### 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:
    - core/StateManager.js, line 265

#### <protected> resume()

  Source:
    - core/StateManager.js, line 510

#### 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:
    - core/StateManager.js, line 236

#### <protected> update()

  Source:
    - core/StateManager.js, line 523