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();
Parameters:
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 inherited from Rect

number
bottom
number
centerX
number
centerY
number
height
number
left
number
right
number
top
string
type = "Rectangle"
number
width

Properties inherited from Polygon

Array<Vector2d>
points

Public Properties


animationpause sprite.js:62
animationpause: boolean = false

boolean
animationspeed sprite.js:68
animationspeed: number = 100

number

animation cycling speed (delay between frame in ms)

isVideo sprite.js:82
isVideo: boolean = false

boolean

true if this is a video sprite (e.g. a HTMLVideoElement was passed as as source)

offset sprite.js:75
offset: Vector2d = <0.0,0.0>

Vector2d

global offset for the position to draw from on the source image.

source sprite.js:96
source: TextureAtlas = undefined

TextureAtlas

The source texture object this sprite object is using

Public Methods


addAnimation sprite.js:302
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");
Parameters:
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

Returns:
Type Description
number

frame amount of frame added to the animation (delay between each frame).

See: Sprite#animationspeed
flicker sprite.js:277
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);
});
Parameters:
Name Type Description
duration number

expressed in milliseconds

callback Function

Function to call when flickering ends

Returns:
Type Description
Sprite

Reference to this object for method chaining

getCurrentAnimationFrame sprite.js:535
getCurrentAnimationFrame() → {number}

return the current animation frame index.

Returns:
Type Description
number

current animation frame index

isCurrentAnimation sprite.js:477
isCurrentAnimation(name: string) → {boolean}

return true if the specified animation is the current one.

if (!this.isCurrentAnimation("walk")) {
    // do something funny...
}
Parameters:
Name Type Description
name string

animation id

Returns:
Type Description
boolean
isFlickering sprite.js:255
isFlickering() → {boolean}

return the flickering state of the object

Returns:
Type Description
boolean
pause sprite.js:270
pause() → {}

play or resume the current animation or video

play sprite.js:263
play() → {}

play or resume the current animation or video

reverseAnimation sprite.js:461
reverseAnimation(name: string) → {Sprite}

reverse the given or current animation if none is specified

Parameters:
Name Type Attributes Description
name string

<optional>

animation id

Returns:
Type Description
Sprite

Reference to this object for method chaining

See: Sprite#animationspeed
setAnimationFrame sprite.js:522
setAnimationFrame(index: number) → {Sprite}

force the current animation frame index.

// reset the current animation to the first frame
this.setAnimationFrame();
Parameters:
Name Type Attributes Default Description
index number

<optional>

0

animation frame index

Returns:
Type Description
Sprite

Reference to this object for method chaining

setCurrentAnimation sprite.js:400
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
 });
Parameters:
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

Returns:
Type Description
Sprite

Reference to this object for method chaining

setRegion sprite.js:490
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"));
Parameters:
Name Type Description
region object

typically returned through me.Texture.getRegion()

Returns:
Type Description
Sprite

Reference to this object for method chaining

See: Texture.getRegion

Protected Methods


draw sprite.js:619
protected draw(renderer: CanvasRenderer | WebGLRenderer, viewport: Camera2d) → {}

draw this srite (automatically called by melonJS)

Parameters:
Name Type Attributes Description
renderer CanvasRenderer | WebGLRenderer

a renderer instance

viewport Camera2d

<optional>

the viewport to (re)draw

update sprite.js:553
protected update(dt: number) → {boolean}

update function.
automatically called by the game manager game

Parameters:
Name Type Description
dt number

time since the last update in milliseconds.

Returns:
Type Description
boolean

true if the Sprite is dirty


Powered by webdoc!