Module gamejs
gamejs.ready()
is maybe the most important function as it kickstarts your app:
var gamejs = require('gamejs');
ready(function() {
gamejs.logging.info('I am ready!')
});
If you use images or sounds preload all assets with gamejs.preload(['./files/foo.png'])
before calling ready()
.
Also in this module is the Rect
class which is generally useful when dealing with Surfaces and simple rectangles (e.g. for collisions).
Functions
Class Rect
Instance Methods
- clip(Rect)
- clone()
- collideLine(pointA, pointB)
- collidePoint(point)
- collideRect(rect)
- inflate(amount, amount)
- inflateIp(amount, amount)
- move(x, y)
- moveIp(x, y)
- toString()
- union(union)
Instance Properties
Rect (position, dimensions)
Creates a Rect. Rects are used to hold rectangular areas. There are a couple of convinient ways to create Rects with different arguments and defaults.
Any function that requires a gamejs.Rect
argument also accepts any of the constructor value combinations Rect
accepts.
Rects are used a lot. They are good for collision detection, specifying an area on the screen (for blitting) or just to hold an objects position.
The Rect object has several virtual attributes which can be used to move and align the Rect:
top, left, bottom, right topleft, bottomleft, topright, bottomright center width, height w,h
All of these attributes can be assigned to. Assigning to width or height changes the dimensions of the rectangle; all other assignments move the rectangle without resizing it. Notice that some attributes are Numbers and others are pairs of Numbers.
Example
new Rect([left, top]) // width & height default to 0 new Rect(left, top) // width & height default to 0 new Rect(left, top, width, height) new Rect([left, top], [width, height]) new Rect(oldRect) // clone of oldRect is created
Parameters
Array|gamejs.Rect | position | Array holding left and top coordinates |
Array | dimensions | Array holding width and height |
Rect.prototype.bottom
Bottom, Y coordinate
Rect.prototype.bottomleft
Bottom-left Position. You can assign a rectangle form.
Rect.prototype.bottomright
Bottom-right Position. You can assign a rectangle form.
Rect.prototype.center
Center Position. You can assign a rectangle form.
Rect.prototype.clip (Rect)
Return the area in which this Rect and argument Rect overlap.
Parameters
gamejs.Rect | Rect | to clip this one into |
Returns
gamejs.Rect | new Rect which is completely inside the argument Rect, zero sized Rect if the two rectangles do not overlap |
Rect.prototype.clone ()
Returns
gamejs.Rect | A new copy of this rect |
Rect.prototype.collideLine (pointA, pointB)
Parameters
Array | pointA | start point of the line |
Array | pointB | end point of the line |
Returns
true if the line intersects with the rectangle |
See
Rect.prototype.collidePoint (point)
Check for collision with a point.
collidePoint(x,y)
or collidePoint([x,y])
or collidePoint(new Rect(x,y))
Parameters
Array|gamejs.Rect | point | the x and y coordinates of the point to test for collision |
Returns
Boolean | true if the point collides with this Rect |
Rect.prototype.collideRect (rect)
Check for collision with a Rect.
Parameters
gamejs.Rect | rect | the Rect to test check for collision |
Returns
Boolean | true if the given Rect collides with this Rect |
Rect.prototype.height
Height of rectangle
Rect.prototype.inflate (amount, amount)
Grow or shrink the rectangle size
Parameters
Number | amount | to change in the width |
Number | amount | to change in the height |
Returns
gamejs.Rect | inflated rectangle centered on the original rectangle's center |
Rect.prototype.inflateIp (amount, amount)
Grow or shrink this Rect in place - not returning a new Rect like inflate(x, y)
would.
Parameters
Number | amount | to change in the width |
Number | amount | to change in the height |
Rect.prototype.left
Left, X coordinate
Rect.prototype.move (x, y)
Move returns a new Rect, which is a version of this Rect moved by the given amounts. Accepts any rectangle form. as argument.
Parameters
Number|gamejs.Rect | x | amount to move on x axis |
Number | y | amount to move on y axis |
Rect.prototype.moveIp (x, y)
Move this Rect in place - not returning a new Rect like move(x, y)
would.
moveIp(x,y)
or moveIp([x,y])
Parameters
Number|gamejs.Rect | x | amount to move on x axis |
Number | y | amount to move on y axis |
Rect.prototype.right
Right, X coordinate
Rect.prototype.toString ()
Returns
String | Like "[x, y][w, h]" |
Rect.prototype.top
Top, Y coordinate
Rect.prototype.topleft
Top-left Position. You can assign a rectangle form.
Rect.prototype.topright
Top-right Position. You can assign a rectangle form.
Rect.prototype.union (union)
Join two rectangles
Parameters
gamejs.Rect | union | with this rectangle |
Returns
gamejs.Rect | rectangle containing area of both rectangles |
Rect.prototype.width
Width of rectangle
Rect.prototype.y
Position x value, alias for left
.
onTick (callbackFunction, callbackScope)
The function passed to onTick
will continously be called at a frequency determined by the browser (typically between 1 and 60 times per second).
Parameters
Function | callbackFunction | the function you want to be called |
Function | callbackScope | optional scope for the function call |
ready (callbackFunction)
ReadyFn is called once all modules and assets are loaded.
Parameters
Function | callbackFunction | the function to be called once gamejs finished loading |