GameLogic.GameState Class
The GameState class provides an object which handle severals methods which can be called by the GameStateStack. Each GameState object contains some methods :
- onUpdate this method is called when the GameState is updating.
- onDraw this method is called when the GameState is drawing
- onCreation this method is called when the GameState is added to a GameStateStack
- onDelete this method is called when the GameState is removed from a GameStateStack
- onSleep this method is called when the GameState looses the focus.
- onWakeUp this method is called when the GameState takes back the focus.
There are two ways to define these methods:
If you have a lot of code for the state, you can inherit from
GameState
and override these methods. Or more simply directly redefine a method of an instance :var myGameState = new GameState(); myGameState.onUpdate = myOnUpdateFunc; myGameState.onDraw = myOnDrawFunc;
For a little state, with just few code, methods can be passed in arguments:
new GameState({ onUpdate: function(elapsedTime) { //before update }, onDraw() { //before draw } });
You can also insert Layers into the GameState. You can sort them by their z-index in asc or desc order. Layers allows you to render something on the context of the GameStateStack.
You can also insert Callbacks into the GameState some callbacks which will be executed when the GameState is updating. Notice that callbacks are executed after the onUpdate event. You can sort the callbacks by specifiying a priority order.
Note that you can also interact with the GameStateStack which own the GameState object. You can : - push states - pop states - go to a special state in the stack
Here is an example on which i show how you can push state, pop state and go to a special state :
this.getGameStateStack().push(newState);
this.getGameStateStack().pop();
this.getGameStateStack().goToState("state_name");
Constructor
GameLogic.GameState
-
params
Parameters:
-
params
Objectthis object should contain severals members
-
[name]
String optionalwhich is the name of the State.
-
[sortLayerAsc=true]
Boolean optionalwhich is a boolean. It must be equal to true if you want to sort Layers by ascendant order. Otherwise it must be equal to false. Default value equals true.
-
[sortCallbackAsc=true]
Boolean optionalwhich is a boolean. It must be equal to true if you want to sort Callbacks by ascendant order. Otherwise it must be equal to false. default value equals true.
-
[onUpdate]
Function optionalcalled when the GameState is updating.
-
[onDraw]
Function optionalcalled when the GameState is drawing.
-
[onCreation]
Function optionalcalled when the GameState is added to a GameStateStack.
-
[onDelete]
Function optionalcalled when the GameState is removed from a GameStateStack.
-
[onSleep]
Function optionalcalled when the GameState looses the focus.
-
[onWakeUp]
Function optionalcalled when the GameState takes back the focus.
-
Item Index
Methods
Properties
Methods
addCallback
-
param
The addCallback function allow you to add a callback to the current GameState object.
Parameters:
-
param
ObjectThis object must contain a number called 'priority' and a reference to the callback called 'callback'.
-
[priority]
Number optionalrepresent the execution priority of the callback.
-
[callback]
Function optionalrepresent the function to execute.
-
Returns:
addLayer
-
layer
The addLayer function allow you to add a Layer to the GameState. By default the addLayer method order Layers by ascendant order by their z-depth values.
Parameters:
-
layer
TW.Graphic.Layerthe Layer which will have to be added to the GameState.
draw
-
canvasContext
This method is private, you do not have to use it, it is used internally by the GameStateStack class.
Parameters:
-
canvasContext
CanvasRenderingContext2DgraphicalContext on which graphical contents will be drawn.
getGameStateStack
()
GameStateStack
This method allows you to get the GameStateStack pattern which the current gameState belongs to.
Returns:
onCreation
()
method called when the state is created and placed on the stack. Can be overridden or given as argument to the constructor.
onDelete
()
method called when the state is removed from the stack. Can be overridden or given as argument to the constructor.
Note: The state object is not always really deleted, and can be reused later. This method should put the GameState as if it has never been used.
onDraw
()
method called before each draw. Can be overridden or given as argument to the constructor.
onSleep
()
method called when the state is put to sleep (another state becomes active). Can be overridden or given as argument to the constructor.
onUpdate
-
elapsedTime
method called before each update. Can be overridden or given as argument to the constructor.
Parameters:
-
elapsedTime
Numberrepresents the amount of milliseconds elapsed since the last update call.
onWakeUp
()
method called when the state becomes active. Can be overridden or given as argument to the constructor.
removeCallback
-
refCallback
This method allows you to remove a callback from the current GameState object.
Parameters:
-
refCallback
Functiona reference to the callback function to remove from the current GameState object.
Returns:
removeLayer
-
refLayer
This method allows you to remove a layer from the GameState.
Parameters:
-
refLayer
TW.Graphic.Layera reference to the layer that you want to suppress from the GameState.
Returns:
setGameStateStack
-
gameStateStack
This method allows you to set the GameStateStack parent of the gameState. Note that this method is use internally by the GameStateStack implementation. You should not use it from your own.
Parameters:
-
gameStateStack
GameStateStackrepresents the gameStateStack object which the GameState object belongs to.
sortCallbacks
()
The sortCallbacks method allow you yo sort callbacks by their priority member.
sortLayers
()
The sortLayers method allow you to sort layers by their z-order.
update
-
elapsedTime
This method is private, you do not have to use it, it is used internally by the GameStateStack class.
Parameters:
-
elapsedTime
Numbertime elapsed since last update call.
Properties
name
String
the name which is associated to the current GameState
sortCallbackAsc
Boolean
The setCallbackOrder order allows you to define the sort order of the Callbacks
If it is true
the layers will be sort by ascendant order.
Otherwise, the layers will be sorted by descendant order.
sortLayerAsc
Boolean
The setSortLayer order allows you to define the sort order of the Layers. Note that the Layers are ordered by their z-index values.
If it is true
the layers will be sort by ascendant order.
Otherwise, your layers will be sorted by descendant order.