Module gamejs/sprite
Provides Sprite the basic building block for any game and
SpriteGroups, which are an efficient
way for doing collision detection between groups as well as drawing layered
groups of objects on the screen.
Functions
- collideCircle(spriteA, spriteB)
- collideMask(spriteA, spriteB)
- collideRect(spriteA, spriteB)
- groupCollide(groupA, groupB, doKillA, doKillB, collided)
- spriteCollide(sprite, group, doKill, collided)
Group ()
Sprites are often grouped. That makes collision detection more efficient and improves rendering performance. It also allows you to easly keep track of layers of objects which are rendered to the screen in a particular order.
Group.update() calls update() on all the contained sprites; the same is true for draw().
Group.prototype.add (sprites)
Add one or more sprites to this group
Parameters
| Array|gamejs.sprite.Sprite | sprites | One or more `gamejs.sprite.Sprite` instances |
Group.prototype.clear (destination, source)
Draw background (source argument) over each sprite in the group
on the destination surface.
This can, for example, be used to clear the display surface to a a static background image in all the places occupied by the sprites of all group.
Parameters
| gamejs.Surface | destination | the surface to draw on |
| gamejs.Surface | source | surface |
Group.prototype.draw ()
Draw all the sprites in this group. This is equivalent to calling each sprite's draw method.
Group.prototype.empty ()
Remove all sprites from this group
Group.prototype.forEach (callback, thisArg)
Loop over each sprite in this group. This is a shortcut for
group.sprites().forEach(...).
Parameters
| callback | ||
| thisArg |
Group.prototype.has (sprites)
Check for the existence of one or more sprites within a group
Parameters
| Array|gamejs.sprite.Sprite | sprites | One or more `gamejs.sprite.Sprite` instances |
Returns
| Boolean | True if every sprite is in this group, false otherwise |
Group.prototype.remove (sprites)
Remove one or more sprites from this group
Parameters
| Array|gamejs.sprite.Sprite | sprites | One or more `gamejs.sprite.Sprite` instances |
Group.prototype.some (callback, thisArg)
Check whether some sprite in this group passes a test. This is a shortcut
for group.sprites().some(...).
Parameters
| callback | ||
| thisArg |
Group.prototype.sprites ()
Get the sprites in this group
Returns
| Array | An array of `gamejs.sprite.Sprite` instances |
Group.prototype.update ()
Update all the sprites in this group. This is equivalent to calling the update method on each sprite in this group.
Sprite ()
Your visible game objects will typically subclass Sprite. By setting it's image
and rect attributes you can control its appeareance. Those attributes control
where and what Sprite.draw(surface) will blit on the the surface.
Your subclass should overwrite update(msDuration) with its own implementation.
This function is called once every game tick, it is typically used to update
the status of that object.
Sprite.prototype.add (groups)
Add the sprite to the passed groups
Parameters
| Array|gamejs.sprite.Group | groups | One or more `gamejs.sprite.Group` instances |
Sprite.prototype.draw (surface)
Draw this sprite onto the given surface. The position is defined by this sprite's rect.
Parameters
| gamejs.Surface | surface | The surface to draw on |
Sprite.prototype.groups ()
Returns an array of all the Groups that contain this Sprite.
Returns
| Array | an array of groups |
Sprite.prototype.image
Image to be rendered for this Sprite.
Sprite.prototype.isDead ()
Returns
| Boolean | True if this sprite has had `Sprite.kill()` called on it previously, otherwise false |
Sprite.prototype.kill ()
Kill this sprite. This removes the sprite from all associated groups and
makes future calls to Sprite.isDead() return true
Sprite.prototype.rect
Rect describing the position of this sprite on the display.
Sprite.prototype.remove (groups)
Remove the sprite from the passed groups
Parameters
| Array|gamejs.sprite.Group | groups | One or more `gamejs.Group` instances |
Sprite.prototype.update ()
Update this sprite. You should override this method with your own to update the position, status, etc.
collideCircle (spriteA, spriteB)
Collision detection between two sprites using circles at centers.
There sprite property radius is used if present, otherwise derived from bounding rect.
Parameters
| gamejs.sprite.Sprite | spriteA | First sprite to check |
| gamejs.sprite.Sprite | spriteB | Second sprite to check |
Returns
| Boolean | True if they collide, false otherwise |
collideMask (spriteA, spriteB)
Collision detection between two sprites utilizing the optional mask
attribute on the sprites. Beware: expensive operation.
Parameters
| gamejs.sprite.Sprite | spriteA | Sprite with 'mask' property set to a `gamejs.mask.Mask` |
| gamejs.sprite.Sprite | spriteB | Sprite with 'mask' property set to a `gamejs.mask.Mask` |
Returns
| Boolean | True if any mask pixels collide, false otherwise |
collideRect (spriteA, spriteB)
Check for collisions between two sprites using their rects.
Parameters
| gamejs.sprite.Sprite | spriteA | First sprite to check |
| gamejs.sprite.Sprite | spriteB | Second sprite to check |
Returns
| Boolean | True if they collide, false otherwise |
groupCollide (groupA, groupB, doKillA, doKillB, collided)
Find all Sprites that collide between two Groups.
Example
groupCollide(group1, group2).forEach(function (collision) {
var group1Sprite = collision.a;
var group2Sprite = collision.b;
// Do processing here!
});
Parameters
| gamejs.sprite.Group | groupA | First group to check |
| gamejs.sprite.Group | groupB | Second group to check |
| Boolean | doKillA | If true, kill sprites in the first group when collided |
| Boolean | doKillB | If true, kill sprites in the second group when collided |
| function | collided | Collision function to use, defaults to `gamejs.sprite.collideRect` |
Returns
| Array | A list of objects where properties 'a' and 'b' that correspond with objects from the first and second groups |
spriteCollide (sprite, group, doKill, collided)
Find sprites in a group that intersect another sprite
Parameters
| gamejs.sprite.Sprite | sprite | The sprite to check |
| gamejs.sprite.Group | group | The group to check |
| Boolean | doKill | If true, kill sprites in the group when collided |
| function | collided | Collision function to use, defaults to `gamejs.sprite.collideRect` |
Returns
| Array | An array of `gamejs.sprite.Sprite` instances that collided |