Sprite
class Sprite extends Renderable
An object to display a fixed or animated sprite on screen.
Constructor
new Sprite(x: number, y: number, settings: object) → {}
// create a single sprite from a standalone image, with anchor in the center
let sprite = new me.Sprite(0, 0, {
image : "PlayerTexture",
framewidth : 64,
frameheight : 64,
anchorPoint : new me.Vector2d(0.5, 0.5)
});
// create a single sprite from a packed texture
mytexture = new me.TextureAtlas(
me.loader.getJSON("texture"),
me.loader.getImage("texture")
);
let sprite = new me.Sprite(0, 0, {
image : mytexture,
region : "npc2.png",
});
// create a video sprite
let videoSprite = new me.Sprite(0, 0, {
image : me.loader.getVideo("bigbunny"),
anchorPoint : new me.Vector2d(0.5, 0.5)
});
// scale the video sprite
videoSprite.currentTransform.scale(2);
// start playing the video (if video is preloaded with `autoplay` set to false)
videoSprite.play();
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
x | number |
the x coordinates of the sprite object |
||
y | number |
the y coordinates of the sprite object |
||
settings | object |
Configuration parameters for the Sprite object |
||
settings.image | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | TextureAtlas | string |
reference to spritesheet image, a texture atlas, a video element, or to a texture atlas |
||
settings.name | string |
<optional> |
"" |
name of this object |
settings.region | string |
<optional> |
region name of a specific region to use when using a texture atlas, see TextureAtlas |
|
settings.framewidth | number |
<optional> |
Width of a single frame within the spritesheet |
|
settings.frameheight | number |
<optional> |
Height of a single frame within the spritesheet |
|
settings.tint | string | Color |
<optional> |
a tint to be applied to this sprite |
|
settings.flipX | number |
<optional> |
flip the sprite on the horizontal axis |
|
settings.flipY | number |
<optional> |
flip the sprite on the vertical axis |
|
settings.anchorPoint | Vector2d |
<optional> |
{x:0.5, y:0.5} |
Anchor point to draw the frame at (defaults to the center of the frame). |
Summary
Properties from Sprite
boolean |
|
number |
|
boolean |
|
Vector2d |
|
TextureAtlas |
|
Methods from Sprite
number |
|
Sprite |
|
number |
|
boolean |
|
boolean |
|
play or resume the current animation or video |
|
play or resume the current animation or video |
|
Sprite |
|
Sprite |
|
Sprite |
|
Sprite |
|
|
|
boolean |
|
Properties inherited from Renderable
number |
|
boolean |
|
Container | Entity |
|
ObservableVector2d |
|
boolean |
|
string |
|
Body |
|
Matrix2d |
|
number |
|
boolean |
|
string |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
Rect | RoundRect | Polygon | Line | Ellipse |
|
string |
|
Function |
|
Application |
|
ObservableVector3d |
|
GLShader |
|
Color |
|
boolean |
|
Properties inherited from Rect
Methods inherited from Renderable
Methods inherited from Rect
Rect |
|
Rect |
|
boolean |
|
Rect |
|
boolean |
|
boolean |
|
boolean |
|
Rect |
|
Rect |
|
Polygon |
|
Rect |
|
Methods inherited from Polygon
Array<number> |
|
boolean |
|
Polygon |
|
Polygon |
|
|
|
Polygon |
|
Polygon |
|
Polygon |
|
Public Properties
animationpause: boolean = false
boolean
animationspeed: number = 100
number
animation cycling speed (delay between frame in ms)
isVideo: boolean = false
boolean
true if this is a video sprite (e.g. a HTMLVideoElement was passed as as source)
offset: Vector2d = <0.0,0.0>
Vector2d
global offset for the position to draw from on the source image.
source: TextureAtlas = undefined
TextureAtlas
The source texture object this sprite object is using
Public Methods
addAnimation(name: string, index: Array<number> | Array<string> | Array<object>, animationspeed: number) → {number}
add an animation
For fixed-sized cell sprite sheet, the index list must follow the
logic as per the following example :
// walking animation
this.addAnimation("walk", [ 0, 1, 2, 3, 4, 5 ]);
// standing animation
this.addAnimation("stand", [ 11, 12 ]);
// eating animation
this.addAnimation("eat", [ 6, 6 ]);
// rolling animation
this.addAnimation("roll", [ 7, 8, 9, 10 ]);
// slower animation
this.addAnimation("roll", [ 7, 8, 9, 10 ], 200);
// or get more specific with delay for each frame. Good solution instead of repeating:
this.addAnimation("turn", [{ name: 0, delay: 200 }, { name: 1, delay: 100 }])
// can do this with atlas values as well:
this.addAnimation("turn", [{ name: "turnone", delay: 200 }, { name: "turntwo", delay: 100 }])
// define an dying animation that stop on the last frame
this.addAnimation("die", [{ name: 3, delay: 200 }, { name: 4, delay: 100 }, { name: 5, delay: Infinity }])
// set the standing animation as default
this.setCurrentAnimation("stand");
Name | Type | Attributes | Description |
---|---|---|---|
name | string |
animation id |
|
index | Array<number> | Array<string> | Array<object> |
list of sprite index or name defining the animation. Can also use objects to specify delay for each frame, see below |
|
animationspeed | number |
<optional> |
cycling speed for animation in ms |
Type | Description |
---|---|
number |
frame amount of frame added to the animation (delay between each frame). |
flicker(duration: number, callback: Function) → {Sprite}
make the object flicker
// make the object flicker for 1 second
// and then remove it
this.flicker(1000, function () {
world.removeChild(this);
});
Name | Type | Description |
---|---|---|
duration | number |
expressed in milliseconds |
callback | Function |
Function to call when flickering ends |
Type | Description |
---|---|
Sprite |
Reference to this object for method chaining |
getCurrentAnimationFrame() → {number}
return the current animation frame index.
Type | Description |
---|---|
number |
current animation frame index |
isCurrentAnimation(name: string) → {boolean}
return true if the specified animation is the current one.
if (!this.isCurrentAnimation("walk")) {
// do something funny...
}
Name | Type | Description |
---|---|---|
name | string |
animation id |
Type | Description |
---|---|
boolean |
isFlickering() → {boolean}
return the flickering state of the object
Type | Description |
---|---|
boolean |
reverseAnimation(name: string) → {Sprite}
reverse the given or current animation if none is specified
Name | Type | Attributes | Description |
---|---|---|---|
name | string |
<optional> |
animation id |
Type | Description |
---|---|
Sprite |
Reference to this object for method chaining |
setAnimationFrame(index: number) → {Sprite}
force the current animation frame index.
// reset the current animation to the first frame
this.setAnimationFrame();
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
index | number |
<optional> |
0 |
animation frame index |
Type | Description |
---|---|
Sprite |
Reference to this object for method chaining |
setCurrentAnimation(name: string, resetAnim: string | Function, preserve_dt: boolean) → {Sprite}
set the current animation this will always change the animation & set the frame to zero
// set "walk" animation
this.setCurrentAnimation("walk");
// set "walk" animation if it is not the current animation
if (this.isCurrentAnimation("walk")) {
this.setCurrentAnimation("walk");
}
// set "eat" animation, and switch to "walk" when complete
this.setCurrentAnimation("eat", "walk");
// set "die" animation, and remove the object when finished
this.setCurrentAnimation("die", () => {
world.removeChild(this);
return false; // do not reset to first frame
});
// set "attack" animation, and pause for a short duration
this.setCurrentAnimation("die", () => {
this.animationpause = true;
// back to "standing" animation after 1 second
setTimeout(function () {
this.setCurrentAnimation("standing");
}, 1000);
return false; // do not reset to first frame
});
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
name | string |
animation id |
||
resetAnim | string | Function |
<optional> |
animation id to switch to when complete, or callback |
|
preserve_dt | boolean |
<optional> |
false |
if false will reset the elapsed time counter since last frame |
Type | Description |
---|---|
Sprite |
Reference to this object for method chaining |
setRegion(region: object) → {Sprite}
change the current texture atlas region for this sprite
// change the sprite to "shadedDark13.png";
mySprite.setRegion(mytexture.getRegion("shadedDark13.png"));
Name | Type | Description |
---|---|---|
region | object |
typically returned through me.Texture.getRegion() |
Type | Description |
---|---|
Sprite |
Reference to this object for method chaining |
Protected Methods
protected draw(renderer: CanvasRenderer | WebGLRenderer, viewport: Camera2d) → {}
draw this srite (automatically called by melonJS)
Name | Type | Attributes | Description |
---|---|---|---|
renderer | CanvasRenderer | WebGLRenderer |
a renderer instance |
|
viewport | Camera2d |
<optional> |
the viewport to (re)draw |
protected update(dt: number) → {boolean}
update function.
automatically called by the game manager game
Name | Type | Description |
---|---|---|
dt | number |
time since the last update in milliseconds. |
Type | Description |
---|---|
boolean |
true if the Sprite is dirty |