API Docs for: 0.3.0
Show:

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 Object

    this object should contain severals members

    • [name] String optional

      which is the name of the State.

    • [sortLayerAsc=true] Boolean optional

      which 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 optional

      which 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 optional

      called when the GameState is updating.

    • [onDraw] Function optional

      called when the GameState is drawing.

    • [onCreation] Function optional

      called when the GameState is added to a GameStateStack.

    • [onDelete] Function optional

      called when the GameState is removed from a GameStateStack.

    • [onSleep] Function optional

      called when the GameState looses the focus.

    • [onWakeUp] Function optional

      called when the GameState takes back the focus.

Methods

addCallback

(
  • param
)
Boolean

The addCallback function allow you to add a callback to the current GameState object.

Parameters:

  • param Object

    This object must contain a number called 'priority' and a reference to the callback called 'callback'.

    • [priority] Number optional

      represent the execution priority of the callback.

    • [callback] Function optional

      represent the function to execute.

Returns:

Boolean: return true if param contains a priority and a callback members. otherwise it will returns false.

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

    the 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 CanvasRenderingContext2D

    graphicalContext 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:

GameStateStack: if the current GameState object have not already been linked with a GameStateStack it will return null.

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 Number

    represents 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
)
Boolean

This method allows you to remove a callback from the current GameState object.

Parameters:

  • refCallback Function

    a reference to the callback function to remove from the current GameState object.

Returns:

Boolean: if the refCallback have been successfully finded and suppressed then the method will return true. Otherwise it will return false.

removeLayer

(
  • refLayer
)
Boolean

This method allows you to remove a layer from the GameState.

Parameters:

  • refLayer TW.Graphic.Layer

    a reference to the layer that you want to suppress from the GameState.

Returns:

Boolean: if the refLayer have been successfully finded and suppressed from the GameState object it will returns true. Otherwise it will returns false.

setGameStateStack

(
  • gameStateStack
)
private

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 GameStateStack

    represents 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 Number

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