Kiwi.StateManager Class
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
Parameters:
-
game
Kiwi.GameThe game that this statemanager belongs to.
Returns:
Item Index
Methods
Properties
Methods
addState
-
state
-
[switchTo=false]
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
AnyThe Kiwi.State instance to add.
-
[switchTo=false]
Boolean optionalIf set to true automatically switch to the given state after adding it
Returns:
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
Checks to see if a key exists. Internal use only.
Parameters:
-
key
String
Returns:
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
Checks to see if the State passed is valid or not.
Parameters:
-
state
Kiwi.State
Returns:
getState
-
key
Gets a state by the key that is passed.
Parameters:
-
key
String
Returns:
objType
()
String
public
The type of object this is.
Returns:
"StateManager"
onLoadComplete
()
private
Executed when the preloading has completed. Then executes the loadComplete and create methods of the new State.
onLoadProgress
-
percent
-
bytesLoaded
-
file
Is execute whilst files are being loaded by the state.
Parameters:
-
percent
NumberThe current percentage of files that have been loaded. Ranging from 0 - 1.
-
bytesLoaded
NumberThe number of bytes that have been loaded so far.
-
file
Kiwi.Files.FileThe 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
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:
switchState
-
key
-
[state=null]
-
[initParams=null]
-
[createParams=null]
-
[preloadParams=null]
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
StringThe name/key of the state you would like to switch to.
-
[state=null]
Any optionalThe state that you want to switch to. This is only used to create the state if it doesn't exist already.
-
[initParams=null]
Object optionalAny parameters that you would like to pass to the init method of that new state.
-
[createParams=null]
Object optionalAny parameters that you would like to pass to the create method of that new state.
-
[preloadParams=null]
Object optionalAny parameters that you would like to pass to the preload method. Since 1.3.0 of Kiwi.JS
Returns:
Whether the State is going to be switched to or not.
update
()
public
The update loop that is accessible on the StateManager.
Properties
_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.