SpriteSheet Class
Encapsulates the properties and methods associated with a sprite sheet. A sprite sheet is a series of images (usually animation frames) combined into a larger image (or images). For example, an animation consisting of eight 100x100 images could be combined into a single 400x200 sprite sheet (4 frames across by 2 high).
The data passed to the SpriteSheet constructor defines three critical pieces of information:
- The image or images to use.
- The positions of individual image frames. This data can be represented in one of two ways: As a regular grid of sequential, equal-sized frames, or as individually defined, variable sized frames arranged in an irregular (non-sequential) fashion.
- Likewise, animations can be represented in two ways: As a series of sequential frames, defined by a start and end frame [0,3], or as a list of frames [0,1,2,3].
SpriteSheet Format
data = {
// DEFINING IMAGES:
// list of images or image URIs to use. SpriteSheet can handle preloading.
// the order dictates their index value for frame definition.
images: [image1, "path/to/image2.png"],
// DEFINING FRAMES:
// the simple way to define frames, only requires frame size because frames are consecutive:
// define frame width/height, and optionally the frame count and registration point x/y.
// if count is omitted, it will be calculated automatically based on image dimensions.
frames: {width:64, height:64, count:20, regX: 32, regY:64},
// OR, the complex way that defines individual rects for frames.
// The 5th value is the image index per the list defined in "images" (defaults to 0).
frames: [
// x, y, width, height, imageIndex, regX, regY
[0,0,64,64,0,32,64],
[64,0,96,64,0]
],
// DEFINING ANIMATIONS:
// simple animation definitions. Define a consecutive range of frames.
// also optionally define a "next" animation name for sequencing.
// setting next to false makes it pause when it reaches the end.
animations: {
// start, end, next, frequency
run: [0,8],
jump: [9,12,"run",2],
stand: 13
}
// the complex approach which specifies every frame in the animation by index.
animations: {
run: {
frames: [1,2,3,3,2,1]
},
jump: {
frames: [1,4,5,6,1],
next: "run",
frequency: 2
},
stand: { frames: [7] }
}
// the above two approaches can be combined, you can also use a single frame definition:
animations: {
run: [0,8,true,2],
jump: {
frames: [8,9,10,9,8],
next: "run",
frequency: 2
},
stand: 7
}
}
Example
To define a simple sprite sheet, with a single image "sprites.jpg" arranged in a regular 50x50 grid with two animations, "run" looping from frame 0-4 inclusive, and "jump" playing from frame 5-8 and sequencing back to run:
var data = {
images: ["sprites.jpg"],
frames: {width:50, height:50},
animations: {run:[0,4], jump:[5,8,"run"]}
};
var spriteSheet = new createjs.SpriteSheet(data);
var animation = new createjs.BitmapAnimation(spriteSheet);
animation.gotoAndPlay("run");
Constructor
Item Index
Methods
Properties
Events
Methods
_calculateFrames
()
protected
_handleImageLoad
()
protected
addEventListener
-
type
-
listener
Adds the specified event listener. Note that adding multiple listeners to the same function will result in multiple callbacks getting fired.
Example
displayObject.addEventListener("click", handleClick);
function handleClick(event) {
// Click happened.
}
Parameters:
clone
()
SpriteSheet
Returns a clone of the SpriteSheet instance.
Returns:
dispatchEvent
-
eventObj
-
[target]
Dispatches the specified event to all listeners.
Example
// Use a string event
this.dispatchEvent("complete");
// Use an object
var event = {
type: "complete",
foo: "bar"
};
this.dispatchEvent(event);
Parameters:
-
eventObj
Object | StringAn object with a "type" property, or a string type. If a string is used, dispatchEvent will construct a generic event object with the specified type.
-
[target]
Object optionalThe object to use as the target property of the event object. This will default to the dispatching object.
Returns:
getAnimation
-
name
Returns an object defining the specified animation. The returned object has a frames property containing an array of the frame id's in the animation, a frequency property indicating the advance frequency for this animation, a name property, and a next property, which specifies the default next animation. If the animation loops, the name and next property will be the same.
Parameters:
-
name
StringThe name of the animation to get.
Returns:
getAnimations
()
Array
Returns an array of all available animation names as strings.
Returns:
getFrame
-
frameIndex
Returns an object specifying the image and source rect of the specified frame. The returned object has an image property holding a reference to the image object in which the frame is found, and a rect property containing a Rectangle instance which defines the boundaries for the frame within that image.
Parameters:
-
frameIndex
NumberThe index of the frame.
Returns:
getFrameBounds
-
frameIndex
Returns a Rectangle instance defining the bounds of the specified frame relative to the origin. For example, a 90 x 70 frame with a regX of 50 and a regY of 40 would return a rectangle with [x=-50, y=-40, width=90, height=70].
Parameters:
-
frameIndex
NumberThe index of the frame.
Returns:
getNumFrames
-
animation
Returns the total number of frames in the specified animation, or in the whole sprite sheet if the animation param is omitted.
Parameters:
-
animation
StringThe name of the animation to get a frame count for.
Returns:
hasEventListener
-
type
Indicates whether there is at least one listener for the specified event type.
Parameters:
-
type
StringThe string type of the event.
Returns:
initialize
()
protected
removeAllEventListeners
-
[type]
Removes all listeners for the specified type, or all listeners of all types.
Example
// Remove all listeners
displayObject.removeAllEvenListeners();
// Remove all click listeners
displayObject.removeAllEventListeners("click");
Parameters:
-
[type]
String optionalThe string type of the event. If omitted, all listeners for all types will be removed.
removeEventListener
-
type
-
listener
Removes the specified event listener.
Important Note: that you must pass the exact function reference used when the event was added. If a proxy function, or function closure is used as the callback, the proxy/closure reference must be used - a new proxy or closure will not work.
Example
displayObject.removeEventListener("click", handleClick);
Properties
_animations
Unknown
protected
_data
Unknown
protected
_frameHeight
Unknown
protected
_frames
Unknown
protected
_frameWidth
Unknown
protected
_images
Unknown
protected
_loadCount
Unknown
protected
_numFrames
Unknown
protected
_regX
Unknown
protected
_regY
Unknown
protected
onComplete
Function
deprecated
The onComplete callback is called when all images are loaded. Note that this only fires if the images were not fully loaded when the sprite sheet was initialized. You should check the complete property to prior to adding an onComplete handler. Ex.
var sheet = new SpriteSheet(data);
if (!sheet.complete) {
// not preloaded, listen for onComplete:
sheet.onComplete = handler;
}
Events
complete
Dispatched when all images are loaded. Note that this only fires if the images were not fully loaded when the sprite sheet was initialized. You should check the complete property to prior to adding a listener. Ex.
var sheet = new SpriteSheet(data);
if (!sheet.complete) {
// not preloaded, listen for onComplete:
sheet.addEventListener("complete", handler);
}