new GameObject(name, sprite, x, y, xOffsetopt, yOffsetopt)
A game object creates an object with a sprite at a certain position. Once the object is added to a SceneGraph it will be drawn on the canvas.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
name |
Number | the name of the object | ||
sprite |
Number | the sprite for the object (null if undrawable) | ||
x |
Number | the x position of the object on the screen | ||
y |
Number | the y position of the object on the screen | ||
xOffset |
Number |
<optional> |
0 | the x offset of the top left corner of the sprite relative to the object position |
yOffset |
Number |
<optional> |
0 | the y offset of the top left corner of the sprite relative to the object position |
Properties:
| Name | Type | Default | Description |
|---|---|---|---|
name |
String | the name of the object | |
sprite |
Sprite | the object's sprite | |
pos |
Vector | the current position of the object | |
nextX |
Number | the x position of the object after an update | |
nextY |
Number | the y position of the object after an update | |
lastX |
Number | the last x position of the object (before the last update) | |
lastY |
Number | the last y position of the object (before the last update) | |
isClicked |
Boolean | whether the object is currently being clicked | |
isDraggable |
Boolean | false | whether or not the object can be dragged via the mouse |
isClickable |
Boolean | true | whether or not the object can be clicked |
isCollidable |
Boolean | true | whether or not the object collides with anything |
isLooping |
Boolean | false | whether the object should loop around the edges of the screen |
doUpdate |
Boolean | true | whether or not the object should be updated |
direction |
Vector | the velocity of the object | |
angle |
Number | the angle of the object, used for what angle to draw it at |
Members
(readonly) height
Properties:
| Name | Type | Description |
|---|---|---|
height |
Number | height of image |
(readonly) src
Properties:
| Name | Type | Description |
|---|---|---|
src |
String | source of the image |
(readonly) width
Properties:
| Name | Type | Description |
|---|---|---|
width |
Number | width of image |
x
Properties:
| Name | Type | Description |
|---|---|---|
x |
Number | x position of object (gets current position, but sets nextX) |
y
Properties:
| Name | Type | Description |
|---|---|---|
y |
Number | y position of object (gets current position, but sets nextY) |
Methods
calculateAngleFromDirection(dirX, dirY)
Changes the object angle based on the direction given
Parameters:
| Name | Type | Description |
|---|---|---|
dirX |
Number | the x direction |
dirY |
Number | the y direction |
calculateVelocity(speed, angle) → {Vector}
Calculates velocity based on the angle and speed of the object
Parameters:
| Name | Type | Description |
|---|---|---|
speed |
Number | the speed of the object |
angle |
Number | the angle of the object |
Returns:
The new velocity based on the given angle and speed
- Type
- Vector
canCollideWith(other) → {Boolean}
Override this function to determine what this object can and can't collide with.
Parameters:
| Name | Type | Description |
|---|---|---|
other |
GameObject | the object that collided |
Returns:
if the object can collide with other objects
- Type
- Boolean
changeAngle(angle)
Adds an angle amount to the current angle of the object
Parameters:
| Name | Type | Description |
|---|---|---|
angle |
Number | the amount of degrees to add to the current angle |
changeSpriteSheetNumber(number)
Changes the the sprite sheet to a specific sheet number
Parameters:
| Name | Type | Description |
|---|---|---|
number |
Number | the accessor that the sprite sheet will use |
checkForObjectCollide(other) → {Boolean}
Returns true if the object collides with another object
Parameters:
| Name | Type | Description |
|---|---|---|
other |
GameObject | the object to check collisions for. |
Returns:
true if the objects collide
- Type
- Boolean
collideWith(other) → {Boolean}
Overide this function with the event you want to take place when the object collides with another
Parameters:
| Name | Type | Description |
|---|---|---|
other |
GameObject | the object that collided |
Returns:
False if there is no event to take place when a collision has happened.
- Type
- Boolean
constructor(name, sprite, x, y, xOffsetopt, yOffsetopt)
Creates the game object
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
name |
Number | the name of the object | ||
sprite |
Number | the sprite for the object (null if undrawable) | ||
x |
Number | the x position of the object on the screen | ||
y |
Number | the y position of the object on the screen | ||
xOffset |
Number |
<optional> |
0 | the x offset of the top left corner of the sprite relative to the object position |
yOffset |
Number |
<optional> |
0 | the y offset of the top left corner of the sprite relative to the object position |
customPreDraw(game)
Override this function to put in your custom pre-draw for this object. Called at start of pre-draw.
Parameters:
| Name | Type | Description |
|---|---|---|
game |
Game | the game object |
customUpdate(game)
Override this function to put in your custom pre-updates for this object. Called at start of update.
Parameters:
| Name | Type | Description |
|---|---|---|
game |
Game | the game object |
draw(context) → {Boolean}
This function draws the object. Do not override this function. If you wish to add anything to this function, use customePreDraw.
Parameters:
| Name | Type | Description |
|---|---|---|
context |
RenderingContext | the context of the game. |
Returns:
true if sprite is drawn
- Type
- Boolean
mouseDown(game, event)
The mouseDown handler for the object. By default, just sets the isClicked variable to true.
Parameters:
| Name | Type | Description |
|---|---|---|
game |
Game | the game object |
event |
MouseEvent | the actual mouse event |
mouseUp(game, event) → {Boolean}
The mouseUp handler for the object. By default, just sets the isClicked variable to false.
Parameters:
| Name | Type | Description |
|---|---|---|
game |
Game | the game object |
event |
MouseEvent | the actual mouse event |
Returns:
True if the mouse is currently not clicking on the object
- Type
- Boolean
pointCollide(x, y) → {Boolean}
Returns true if the object collides at a specific point.
Parameters:
| Name | Type | Description |
|---|---|---|
x |
Number | x position to check |
y |
Number | y position to check |
Returns:
true if the object collides with the specific position
- Type
- Boolean
postUpdate(game)
Override this function to put in your custom post updates for this object. Called after update.
Parameters:
| Name | Type | Description |
|---|---|---|
game |
Game | the game object |
setCircleHitbox(centeropt, radiusopt)
Use this function to set a Circular Hitbox for the object
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
center |
Vector |
<optional> |
halfway point of sprite | The center of the circle |
radius |
Number |
<optional> |
Math.max(this.width, this.height)/2 | The radius of the circle |
setSquareHitbox(xRangeopt, yRangeopt)
Use this function to set a Square Hitbox for the object
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
xRange |
Array.<Number> |
<optional> |
[0,1] | The x range of which you want the hitbox to be. The array should only have a length of 2, with each number being in the range [0, 1] |
yRange |
Array.<Number> |
<optional> |
[0,1] | The y range of which you want the hitbox to be. The array should only have a length of 2, with each number being in the range [0, 1] |
tryCollide(other) → {Boolean}
Tries to collide with another object. Fails if other object isn't collidable, if hitboxes aren't colliding, or if the objects don't have an applicable collision interaction.
Parameters:
| Name | Type | Description |
|---|---|---|
other |
GameObject | the object that collided |
Returns:
returns true if the object successfully collides with another object
- Type
- Boolean
update(game)
The update function of the object. Do not override this function. Handles looping, velocity, and dragging
Parameters:
| Name | Type | Description |
|---|---|---|
game |
Game | The game object |
updatePosition()
Call this function to update the object's position (moves position from nextX and nextY to current position)