Class: Game

Game(width, height, divID)

The Game class sets up the canvas where the game appears, manages a collection of Screen objects, which contain the code for each individual game screen, and manages up the Input object and Clock objects, shared by all screens.
Classes which extend Game must implement the initialize method.

Constructor

new Game(width, height, divID)

Set up the canvas where the game will appear.
Parameters:
Name Type Default Description
width number 800 the width of the canvas containing the game
height number 600 the height of the canvas containing the game
divID string null the ID of the HTML div element where the canvas should be placed; if not included, a div element will be created
Source:

Methods

addScreen(screenName, screenObject)

Add a Screen object to this game.
Parameters:
Name Type Description
screenName string the name that should be used to refer to the screen when using the setScreen method
screenObject Screen the screen to be added to the game
Source:

clearCanvas(clearColor)

Clear the game canvas by drawing a colored rectangle that fills the canvas.
Parameters:
Name Type Default Description
clearColor string #000000 name or hex code of color to use when clearing the canvas
Source:

getData()

Retrieve data previously stored by setData.
Source:

initialize()

Add Screen objects and set first screen to display.
Must be implemented by extending class.
Source:

quit()

Quit the game: stop the game loop, stop the game from listening for user keyboard input, and clear the canvas.
Source:

setData(dataName, dataValue)

Store data at a global level so it is accessible to all screens via getData.
Note: required data must be set before screens are created.
Parameters:
Name Type Description
dataName string name used to reference and retrieve data later
dataValue object value to be stored
Source:

setScreen(screenName)

Set the active Screen object for this game.
During each frame, the game calls the Screen.update method and renders the Sprites in the Group of the active screen.
Parameters:
Name Type Description
screenName string the name of the associated screen initially specified by the addScreen method
Source:

setScreenFadeTransition(screenFadeTransition, screenFadeDuration, screenFadeColor)

Determine whether current screen will fade out and next screen will fade in, when the screen is changed using setScreen, and set the screen fade duration and color.
Parameters:
Name Type Default Description
screenFadeTransition boolean true whether or not screens will fade in and fade out
screenFadeDuration number 1 the amount of time to fade in and fade out
screenFadeColor string #000000 the color of the fade between screens
Source:

start()

Start the game: create game objects and run the initialize method.
Source:

update()

Update the game: update the game Input and Clock objects, run the active screen's update method, and draw the active screen's sprite images to the canvas.
Source: