API Docs for: 1.4.0
Show:

Kiwi.StateManager Class

Module: Kiwi

The State Manager handles the starting, parsing, looping and swapping of game States within a Kiwi Game There is only ever one State Manager per game, but a single Game can contain multiple States.

Constructor

Kiwi.StateManager

(
  • game
)
Kiwi.StateMananger

Parameters:

  • game Kiwi.Game

    The game that this statemanager belongs to.

Returns:

Kiwi.StateMananger:

Methods

addState

(
  • state
  • [switchTo=false]
)
Boolean public

Adds the given State to the StateManager. The State must have a unique key set on it, or it will fail to be added to the manager. Returns true if added successfully, otherwise false (can happen if State is already in the StateManager)

Parameters:

  • state Any

    The Kiwi.State instance to add.

  • [switchTo=false] Boolean optional

    If set to true automatically switch to the given state after adding it

Returns:

Boolean:

true if the State was added successfully, otherwise false

boot

() public

Is executed once the DOM has finished loading. This is an INTERNAL Kiwi method.

bootNewState

() private

Actually switches to a state that is stored in the 'newStateKey' property. This method is executed after the update loops have been executed to help prevent developer errors.

callCreate

() private

Checks to see if the state being switched to contains a create method. If it does then it calls the create method.

checkInit

() private

Checks to see if the state has a init method and then executes that method if it is found.

checkKeyExists

(
  • key
)
Boolean private

Checks to see if a key exists. Internal use only.

Parameters:

  • key String

Returns:

Boolean:

checkPreload

() private

Checks to see if the state that is being switched to needs to load some files or not. If it does it loads the file, if it does not it runs the create method.

checkValidState

(
  • state
)
Boolean private

Checks to see if the State passed is valid or not.

Parameters:

Returns:

Boolean:

getState

(
  • key
)
Kiwi.State private

Gets a state by the key that is passed.

Parameters:

  • key String

Returns:

objType

() String public

The type of object this is.

Returns:

String:

"StateManager"

onLoadComplete

() private

Executed when the preloading has completed. Then executes the loadComplete and create methods of the new State.

onLoadProgress

(
  • percent
  • bytesLoaded
  • file
)
private

Is execute whilst files are being loaded by the state.

Parameters:

  • percent Number

    The current percentage of files that have been loaded. Ranging from 0 - 1.

  • bytesLoaded Number

    The number of bytes that have been loaded so far.

  • file Kiwi.Files.File

    The last file that has been loaded.

postRender

() public

PostRender - Called after all of the rendering has been executed in a frame.

rebuildLibraries

() public

Rebuilds the texture, audio and data libraries that are on the current state. Thus updating what files the user has access to.

setCurrentState

(
  • key
)
Boolean private

Switches to the name (key) of the state that you pass. Does not work if the state you are switching to is already the current state OR if that state does not exist yet.

Parameters:

  • key String

Returns:

Boolean:

switchState

(
  • key
  • [state=null]
  • [initParams=null]
  • [createParams=null]
  • [preloadParams=null]
)
Boolean public

Swaps the current state. If the state has already been loaded (via addState) then you can just pass the key. Otherwise you can pass the state object as well and it will load it then swap to it.

Parameters:

  • key String

    The name/key of the state you would like to switch to.

  • [state=null] Any optional

    The state that you want to switch to. This is only used to create the state if it doesn't exist already.

  • [initParams=null] Object optional

    Any parameters that you would like to pass to the init method of that new state.

  • [createParams=null] Object optional

    Any parameters that you would like to pass to the create method of that new state.

  • [preloadParams=null] Object optional

    Any parameters that you would like to pass to the preload method. Since 1.3.0 of Kiwi.JS

Returns:

Boolean:

Whether the State is going to be switched to or not.

update

() public

The update loop that is accessible on the StateManager.

Properties

_game

Kiwi.Game private

The game that this manager belongs to.

_newStateKey

String private

The name of the new State that is to be switched to.

Default: null

_states

Array private

An array of all of the states that are contained within this manager.

current

Kiwi.State public

The current State that the game is at.

Default: null