Table of Contents generated with DocToc
- button
- resources
- vec2d
- Animation
- Batch
- Engine
- Label
- Sound
- Sprite
- methods
- new Sprite(animation, params)
- setAnimation(animation)
- draw(context)
- getSize()
- getAnchor()
- getTopLeft()
- getBottomRight()
- getTop()
- getLeft()
- getBottom()
- getRight()
- setLeft(x)
- setRight(x)
- setTop(y)
- setBottom(y)
- isTouching(sprite)
- hitTest(vec2d)
- setVisible(visible)
- setZOrder(zOrder)
- setFrameIndex(frameIndex)
- setLoop(loop)
- setAnimationStartDate(animationStartDate)
- getFrameIndex()
- delete()
- properties
- events
- methods
var button = require('chem').button;
Enumeration to get the button that a person is activating. Keyboard buttons
start with Key
and mouse buttons start with Mouse
.
For example, the left mouse button is chem.button.MouseLeft
and the
right arrow key is chem.button.KeyRight
.
See also:
Engine::buttonState
Engine::buttonJustPressed
Engine::buttonJustReleased
Engine:: 'buttondown' event (button)
Engine:: 'buttonup' event (button)
See lib/button.js
for the full listing.
var resources = require('chem').resources;
Object containing all the animation metadata, indexed by name.
Spritesheet Image.
Contains a map of your static image resources. These are in public/img.
Contains a map of your text resources. These are in public/text.
Path to prefix asset urls with.
Returns the correct resource url, taking into account prefix, based on a relative asset path.
callback(err, textFileContents)
callback(err, image)
These are events that you can subscribe to:
resources.on('eventName', function() {
// your code here
});
For more information see EventEmitter
Emitted when all resources are loaded and you may begin utilizing them.
complete
- how many resources have been fetchedtotal
- total number of resources to fetch
var vec2d = require('chem').vec2d;
See node-vec2d
var Animation = require('chem').Animation;
An animation is a list of frames backed by a spritesheet from which to draw them.
Creates a new animation with no frames.
json
has these properties:
delay
- how many seconds between each framespritesheet
- the Image from which to draw frames when using this animationanchor
- vec2d instance representing the center point of rotation, scaling, and positioning.loop
frames
- an array of{pos, size}
objects which are the locations in the spritesheet of each of the frames.pos
andsize
are vec2d instances.
image
- the image which this animation will drawoptions
- optional. can contain:anchor
- seeanchor
property
pos
- vec2d instance representing the upper left corner of the image in the spritesheetsize
- vec2d instance containing the width and height of the image in the spritesheet
Deletes the frame at index
.
Turns this animation into an empty animation with no frames.
delay
is the amount of seconds you want in between each frame.
Extracts the image at frameIndex
from the spritesheet and returns it.
Read-only. Number of seconds between each frame.
Read/write. The Image from which to draw frames when using this animation.
Read-only. Total length in seconds of this animation.
Read/write. vec2d instance representing the center point of rotation, scaling, and positioning.
Read-only. An array of {pos, size}
objects which specify where in the
spritesheet the frame draws from.
var Batch = require('chem').Batch;
A Batch
is a set of sprites and/or labels which you can draw together.
Adds a sprite or label to the batch. If called multiple times for the same item, the item is only added once.
item
-sprite
orlabel
to add
Removes an item from the batch. OK to call multiple times.
item
-sprite
orlabel
to remove
Draw all the sprites and labels in the batch.
All of the sprites and labels will be drawn with the correct
rotations, offsets, scaling, zOrder
, and animation applied.
context
- 2D canvas drawing context.
Deletes all sprites in the batch.
See also Sprite::delete()
var Engine = require('chem').Engine;
Create an instance of the engine and bind it to a canvas:
var engine = new chem.Engine(document.getElementById("the-canvas"));
Resize the game.
Call this to start the main loop and start listening to events.
If the FPS drops below this value, your engine will lag instead of
trying to compensate with a larger dt/dx sent to update
Defaults to 20
.
See also Engine:: 'update' event (dt, dx)
Call this to stop the main loop and stop listening to events.
Return true if and only if button
is currently pressed. See also button
.
Call from the update
event. It returns true
for the 1 frame
after the button was pressed.
See also button
.
Call from the update
event. It returns true
for the 1 frame
after the button was released.
See also button
.
Creates a Label
which is a frames per second display. Draw it with
Label::draw(context)
.
See also Label
.
Read only. Vec2d
instance. Use Engine::setSize
if you want to update
the size.
Read only. Contains an up to date value for the current frames per second.
Read only. The canvas element that the engine is using.
Read only. Vec2d
instance representing the current mouse position
relative to the canvas.
Read/write. This is an object which is initially empty and contains buttons which the game should bubble up instead of capturing.
Example:
// now you can press Ctrl+R, etc
engine.buttonCaptureExceptions[chem.button.KeyCtrl] = true;
These are events that you can subscribe to:
engine.on('eventName', function() {
// your code here
});
For more information see EventEmitter
Fired as often as possible, capping at about 60 FPS. Use it to compute the next frame in your game.
For some games it makes sense to ignore dt
and dx
and compute the
next frame in a fixed timestep.
dt
- "delta time" - the amount of time in seconds that has passed sinceupdate
was last fired.dx
- a multiplier intended to adjust your physics. If your game is running perfectly smoothly at 60 FPS,dx
will be exactly 1. If your game is running half as fast as it should, at 30 FPS,dx
will be 2.dx
is equal todt
* 60.
See also setMinFps(fps)
.
You should perform all canvas drawing based on your game state in response to this event.
See also Batch::draw
.
Fired when the mouse moves.
pos
- aVec2d
instance.button
- an enum frombutton
.
See also Engine::mousePos
.
Fired when the player presses a button down.
button
: an enum frombutton
.
Fired when the player releases a button.
button
: an enum frombutton
.
var Label = require('chem').Label;
A Label is a piece of text that you want to display somewhere on the screen.
Example:
var label = new chem.Label("hello world", {
pos: new Vec2d(0, 0),
scale: new Vec2d(1, 1),
zOrder: 0,
batch: some_batch,
rotation: 0,
visible: true,
alpha: 1,
font: "10px sans-serif",
textAlign: "start",
textBaseline: "alphabetic",
fill: true,
fillStyle: "#000000",
stroke: false,
lineWidth: 1,
strokeStyle: "#000000",
});
All the parameters are optional.
Draws the label onto the context. Most of the time you will not
do this directly; instead you will add the label to a batch
and call batch.draw(context)
.
See Batch
.
Hides the label but keeps it ready to display again.
See also delete()
.
Use to layer labels and sprites the way you want to. Start with 0 as the bottom layer. When you want to put something on top, use 1 for the zOrder, 2 for something on top of that, and so on.
See also Label::zOrder
Indicates that the label should release all resources and no longer display on the canvas.
See also setVisible(visible)
.
Vec2d
. Get or set the position in the canvas this label is drawn.
Read only. Vec2d
width and height of the first frame, not taking into
account scale
or current frame.
Vec2d
. Get or set the scale with which the label is drawn.
Read only. Use Label::setZOrder
to change the zOrder
of a label.
Read only. Use Batch::add(label)
and Batch::remove(label)
to
change this value.
Get or set the angle of the label, in radians. Going over 2 * pi is OK.
Get or set the opacity on a scale of 0 to 1.
Get or set the font used to render.
Examples:
- "10px Arial"
- "12pt sans-serif"
Get or set the alignment value. Possible values:
left
- The text is left-aligned.right
- The text is right-aligned.center
- The text is centered.start
- The text is aligned at the normal start of the line (left-aligned for left-to-right locales, right-aligned for right-to-left locales).end
- The text is aligned at the normal end of the line (right-aligned for left-to-right locales, left-aligned for right-to-left locales).
Default is start
.
Get or set the text baseline being used when drawing text. Possible values:
top
- The text baseline is the top of the em square.middle
- The text baseline is the middle of the em square.alphabetic
- The text baseline is the normal alphabetic baseline.ideographic
- The text baseline is the ideographic baseline; this is the bottom of the body of the characters, if the main body of characters protrudes beneath the alphabetic baseline. Currently unsupported; this will act like alphabetic.bottom
- The text baseline is the bottom of the bounding box. This differs from the ideographic baseline in that the ideographic baseline doesn't consider descenders.
Default is alphabetic
.
Get or set boolean whether to fill the text. Default true
.
Get or set the color used to fill. Default "#000000"
.
Get or set boolean whether to stroke the text. Default false
.
Get or set the color used to stroke. Default "#000000"
.
Get or set the width of the stroke. Default 1
.
var Sound = require('chem').Sound;
A Sound
is a pool of HTML5 Audio
objects. It abstracts away dealing
with a pool so that you can fire off the same sound effect while it is
already playing.
If you want to create looping background music, you should use Audio
directly.
Create a sound pool based on its URL:
var sound = new chem.Sound('url/to/sound.ogg');
Plays the sound. If the sound is already playing, it will play another
instance at the same time. If the number of instances in the pool is
greater than maxPoolSize
, it will find the least recently played audio
in the pool and restart it.
Returns the HTML5 Audio object that is generating the sound, which has these methods:
pause()
play()
And these properties:
currentTime
duration
Stops all the Audio instances in the pool.
Sets the volume
property. Same as HTML5 Audio object.
Sets the preload
property. Same as HTML5 Audio object.
Sets the playbackRate
property. Same as HTML5 Audio object.
Read-only. URL of the playing sound.
Read-only.
Read-only.
Read-only.
Read/write. Defaults to 10.
Read only. Only valid after the 'ready' event has fired.
Read only. How much audio has been loaded in seconds.
Fired when some more audio has been buffered.
Fired when the sound is done loading.
var Sprite = require('chem').Sprite;
A Sprite
is a graphic that you might want to move around on the screen.
Example:
var sprite = new chem.Sprite(chem.resources.animations.some_animation_name, {
pos: new Vec2d(0, 0),
scale: new Vec2d(1, 1),
zOrder: 0,
batch: some_batch,
rotation: 0,
visible: true,
frameIndex: 0,
loop: true,
alpha: 1,
});
All the parameters are optional.
Changes the sprite's animation to animation
.
Note that you probably also want to call setFrameIndex(0)
if you want
the new animation to start from the beginning.
You can get an animation
from chem.resources.animations
. It is recommended
to use var ani = chem.resources.animations
as an import declaration so
that it is easy to access animations.
See also Animation
.
Draws the sprite onto the context. Most of the time you will not
do this directly; instead you will add the sprite to a batch
and call batch.draw(context)
.
See Batch
.
Like Sprite::size
but takes scale and current frame into account.
Convenience method to get a Vec2d
representing the anchor position.
Takes into account scale.
Does not take into account rotation.
Convenience method returning a Vec2d
instance of the corner of the sprite.
Takes into account scale and current frame.
Does not take into account rotation.
Convenience method returning a Vec2d
instance of the corner of the sprite.
Takes into account scale and current frame.
Does not take into account rotation.
Convenience method returning the location of the edge of the sprite. Takes into account scale and current frame. Does not take into account rotation.
Convenience method returning the location of the edge of the sprite. Takes into account scale and current frame. Does not take into account rotation.
Convenience method returning the location of the edge of the sprite. Takes into account scale and current frame. Does not take into account rotation.
Convenience method returning the location of the edge of the sprite. Takes into account scale and current frame. Does not take into account rotation.
Convenience method to set the location of the edge of the sprite. Takes into account scale and current frame. Does not take into account rotation.
Convenience method to set the location of the edge of the sprite. Takes into account scale and current frame. Does not take into account rotation.
Convenience method to set the location of the edge of the sprite. Takes into account scale and current frame. Does not take into account rotation.
Convenience method to set the location of the edge of the sprite. Takes into account scale and current frame. Does not take into account rotation.
Returns boolean of whether the sprite is colliding with another. Takes into account scale and current frame. Does not take into account rotation.
Returns boolean of whether the point is inside the bounding box of the sprite. Takes into account scale and current frame. Does not take into account rotation.
Hides the sprite but keeps it ready to display again.
See also delete()
.
Use to layer labels and sprites the way you want to. Start with 0 as the bottom layer. When you want to put something on top, use 1 for the zOrder, 2 for something on top of that, and so on.
See also Sprite::zOrder
Set the frame to show when the sprite is rendered. Animation will continue
from this frame as usual. To find out the frame count, use
sprite.animation.frames.length
.
This value overrides the animation's loop property. If set to null
,
it will fall back to the animation's loop property.
See also Sprite::loop
.
Does the same thing as Sprite::setFrameIndex(frameIndex)
except you
specify a date instead of a frame index.
Returns the index of the current frame. See also
setFrameIndex(frameIndex)
Indicates that the sprite should release all resources and no longer display on the canvas.
See also setVisible(visible)
.
Vec2d
. Get or set the position in the canvas this sprite is drawn.
Read only. Vec2d
width and height of the first frame, not taking into
account scale
or current frame.
Vec2d
. Get or set the scale with which the sprite is drawn.
Read only. Use Sprite::setZOrder
to change the zOrder
of a sprite.
Read only. Use Batch::add(sprite)
and Batch::remove(sprite)
to
change this value.
Get or set the angle of the sprite, in radians. Going over 2 * pi is OK.
Read only. Use Sprite::setLoop(loop)
to set this value.
Get or set the opacity on a scale of 0 to 1.
Read only. The current Animation
of the sprite. Set it with setAnimation
.
These are events that you can subscribe to.
See EventEmitter.
Fired when the animation completes. If loop
is true, this will be at a
regular interval. If loop
is false, this will fire only once, until you
reset the frame index.
See also setFrameIndex(frameIndex)
.