new Game(canvas, name)
The main game class
Parameters:
| Name | Type | Description |
|---|---|---|
canvas |
HTMLCanvasElement | the canvas to draw on |
name |
String | the name of the game |
Properties:
| Name | Type | Description |
|---|---|---|
objects |
SceneGraph | object tree |
sprites |
SceneGraph | sprite tree |
mouseX |
Number | x position of mouse |
mouseY |
Number | y position of mouse |
name |
String | name of game |
timer |
Number | id of timer for running game loop |
score |
SceneGraph | tree of score objects |
canvas |
HTMLCanvasElement | canvas to draw on |
context |
RenderingContext | 2d context of canvas |
gameManager |
GameManager | instance of game manager used by game |
Methods
constructor(canvas, name)
Constructor method
Parameters:
| Name | Type | Description |
|---|---|---|
canvas |
HTMLCanvasElement | the canvas to draw on |
name |
String | the name of the game |
customPostDraw()
User-defined custom post draw function. By default, doesn't do anything. Override to use.
customPreDraw()
User-defined custom pre draw function. By default, doesn't do anything. Override to use.
draw()
Draws the game by drawing all of the drawable objects
findRandomUnoccupiedPoint(objectTreeopt, stepopt) → {Array.<Number>}
Finds a random point that is not occupied by any part of any object
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
objectTree |
SceneGraph |
<optional> |
this.objects | the objectTree to check against. If none, defaults to this.objects |
step |
Number |
<optional> |
1 | how large of an increment to use when selecting a random point. If none, defaults to 1 pixel. Useful for grid-based games |
Returns:
a random point that does not hit any objects in the format [x, y]
- Type
- Array.<Number>
getObjectsUnderMouse(objectTreeopt, limitToClickableopt) → {Array.<Number>}
Gets a list of all objects that are under the mouse
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
objectTree |
SceneGraph |
<optional> |
this.objects | what objectTree to check through. Defaults to this.objects |
limitToClickable |
Boolean |
<optional> |
true | whether or not to only check against clickable objects. Defaults to true. |
Returns:
a list of objects under the mouse
- Type
- Array.<Number>
loop()
Game loop. Runs through update and draw process
mouseDown()
Mousedown function. Calls mouseDown on all of the clickable objects in the game
mouseMove()
Mousemove function. Updates Game.mouseX and Game.mouseY
mouseOut()
Mouseout function, called when the mouse leaves the canvas. By default, delgates to Game.MouseDown
mouseUp()
Mouseup function. Calls mouseUp on all of the clickable objects in the game
outOfBounds(x, y) → {Boolean}
Checks if a coordinate is out of the bounds of the canvas
Parameters:
| Name | Type | Description |
|---|---|---|
x |
Number | x coordinate to check |
y |
Number | y coordinate to check |
Returns:
true if coordinate is out of bounds
- Type
- Boolean
postDraw()
Runs after draw call. Calls Game.customPostDraw and then draws the score if applicable
preDraw()
Runs before draw call. Calls Game.customPreDraw
start(milliseconds)
Starts running the game
Parameters:
| Name | Type | Description |
|---|---|---|
milliseconds |
Number | the amount of time that should be set to pass between game loop calls |
stop()
Stops running the game
update()
Update function. In order, Gets key presses, runs all events from the game manager, updates all objects, updates all object states to reflect their new positions, and finally runs post-update events from the game manager.