Class: GameLoop

wk/game/GameLoop~ GameLoop

A class that enables running a game loop which guarantees exactly 60 model updates per second (in most cases), even if the view update rate is significantly lower.

new GameLoop(p_config)

Parameters:
Name Type Description
p_config Object
Properties
Name Type Argument Default Description
update function <nullable>
A model update function that is to be called exactly 60 times per second.
render function <nullable>
A view render function that is to be called as often as possible up to 60 times per second.
init function <nullable>
A hook to do some additional init action, when the game loop has been created
reset function <nullable>
A function to reset the game to an initial state. It is called, whenever the game loop is created or restarted.
finish function <nullable>
A function to finish the game when the game loop is stopped.
panic function <optional>
this.panicDefault A function that is called if the state of the loop is GameLoop.RUNNING and the model update function is not called for at least one second. By default the loop is paused.
document Object <optional>
window.document The document where the views are to be displayed. If the document looses the visibility focus, the game loop automatically is paused. This, however, doesn't work correctly in all browsers.
ups number <optional>
60 The number of model updates per second. The value should be 60 at least.
Source:

Members


<static> CONTINUE :string

The action to continue the game loop.
Type:
  • string
Source:

<static> EVENT_CONTINUED :string

The name of the event GameLoop.EVENT_CONTINUED.
Type:
  • string
Source:

<static> EVENT_PAUSED :string

The name of the event GameLoop.EVENT_PAUSED.
Type:
  • string
Source:

<static> EVENT_RUNNING :string

The name of the event GameLoop.EVENT_RUNNING.
Type:
  • string
Source:

<static> EVENT_STARTED :string

The name of the event GameLoop.EVENT_STARTED.
Type:
  • string
Source:

<static> EVENT_STOPPED :string

The name of the event GameLoop.EVENT_STOPPED.
Type:
  • string
Source:

<static> PAUSE :string

The action to pause the game loop.
Type:
  • string
Source:

<static> PAUSED :string

The state of the game loop when the game loop has been paused by means of the method pause.
Type:
  • string
Source:

<static> RUNNING :string

The state of the game loop when the game loop has been started or continued by means one of the methods start and continue.
Type:
  • string
Source:

<static> START :string

The action to start the game.
Type:
  • string
Source:

<static> STOP :string

The action to stop the game loop.
Type:
  • string
Source:

<static> STOPPED :string

The state of the game loop when the game loop has been stopped by means of the method stop. This is the default state, after the game loop has been initialized.
Type:
  • string
Source:

<static> TOGGLE_PAUSE_CONTINUE :string

The action to toggle between the states GameLoop.PAUSED and GameLoop.RUNNING.
Type:
  • string
Source:

<static> TOGGLE_START_STOP :string

The action to toggle between the states GameLoop.RUNNING and GameLoop.STOPPED.
Type:
  • string
Source:

state :string

The current state of the game loop.
Type:
  • string
Source:

Methods


continue()

Method to continue the game loop. A call of this method has only an effect, if the current state of the game loop is GameLoop.PAUSED.
Source:

panicDefault()

The default panic function. It pauses the game loop.
Source:

pause()

Method to pause the game loop. A call of this method has only an effect, if the current state of the game loop is GameLoop.RUNNING.
Source:

start()

Method to start the game loop. A call of this method has only an effect, if the current state of the game loop is GameLoop.STOPPED.
Source:

stop()

Method to stop the game loop. A call of this method has only an effect if the current state of the game loop is not already GameLoop.STOPPED.
Source:

togglePauseContinue()

Method to toggle between the states GameLoop.PAUSED and GameLoop.RUNNING.
Source:

toggleStartStop()

Method to toggle between the states GameLoop.RUNNING and GameLoop.STOPPED.
Source: