Members
(constant) canvas :HTMLCanvasElement
Type:
- HTMLCanvasElement
(constant) ctx :CanvasRenderingContext2D
Type:
- CanvasRenderingContext2D
(constant) images :Object
A dictionary of images whose keys are the same as Sprite types
Type:
- Object
(constant) inputStates :Object
Local object used by handlers to message the loop
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
inputStates.isUp |
Boolean | Up arrow pressed or not |
inputStates.isThrust |
Boolean | Don't want to add more rocket noise 60 times a second |
inputStates.isLeft |
Boolean | Left arrow pressed or not |
inputStates.isRight |
Boolean | Right arrow pressed or not |
inputStates.isSpace |
Boolean | Space arrow pressed or not |
inputStates.isLoaded |
Boolean | Space bar needs to be released between missiles |
- Source:
(constant) state :Object
The animation loop and event handlers communicate via this global object
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
sprites |
Array.<Sprite> | spaceship and 12 asteroids in list |
- Source:
Methods
animationLoop() → {undefined}
The thing that freezes Jasmine to make this untestable
Returns:
Never returning infinite loop
- Type
- undefined
createAsteroid() → {Sprite}
Initialises an asteroid Sprite
- Source:
Returns:
Selects random start (not on top of spaceship), speed and rotation
- Type
- Sprite
createMissile() → {Sprite}
Initialises a missile Sprite, shoots from tip of spaceship
- Source:
Returns:
Missile shot from spaceship, lives 2 seconds (120 ticks)
- Type
- Sprite
createSpaceship() → {Sprite}
Initialises a spaceship Sprite
- Source:
Returns:
A spaceship starting in the centre, facing up, stationary
- Type
- Sprite
draw(sprite) → {undefined}
Uses five of CanvasRenderingContext2D's many methods
Parameters:
Name | Type | Description |
---|---|---|
sprite |
Sprite | The 9 parameters needed by ctx.drawImage, the 2 for ctx.translate, and angle for ctx.rotate |
Returns:
Causes side-effect of drawing image
- Type
- undefined
getScale(windowWidth, windowHeight, BASE_WIDTH, BASE_HEIGHT) → {float}
Calculate a ratio to make the canvas larger for big screens, smaller for mobile devices
Parameters:
Name | Type | Description |
---|---|---|
windowWidth |
pixels | - originally hardwired to window.innerWidth |
windowHeight |
pixels | - originally hardwired to window.innerHeight |
BASE_WIDTH |
pixels | - originally hardwired to backgroundImage.width |
BASE_HEIGHT |
pixels | - originally hardwired to backgroundImage.height |
Returns:
Amount to multiply the sizes of images
- Type
- float
nextTick(sprite) → {undefined}
The physics/game engine
Parameters:
Name | Type | Description |
---|---|---|
sprite |
Sprite | Position and movement parameters to be updated |
- Source:
Returns:
Mutates the sprite object
- Type
- undefined
playSound(soundName) → {buffer}
Public function to use sound objects
Parameters:
Name | Type | Description |
---|---|---|
soundName |
"background" | "explosion" | "missile" | "thrust" | "backgroundStop" | "thrustStop" | key in sounds dictionary |
Returns:
Return object needed to turn thrust sound off
- Type
- buffer
resizeListener() → {undefined}
The resizeListener event handler
Returns:
Mutates the scale attribute of
- Type
- undefined
uiListener(event) → {undefined}
The user input event handler
Parameters:
Name | Type | Description |
---|---|---|
event |
KeyboardEvent | PointerEvent | Object sent by target.addEventListener(type, (event) => uiListener(inputStates, event)); |
Properties:
Name | Type | Description |
---|---|---|
event.type |
DOMString | Inherited from Event |
event.key |
DOMString | A Key Value |
event.target |
DOMString | document since that's what called this function |
- Source:
Returns:
Mutates the inputStates global object
- Type
- undefined
Type Definitions
sounds
Dictionary of sound objects
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
audioNode |
buffer | Binary blobs from sound files |