[Phaser] 3. Sprite

Insert Image Into Library

game.load.image(library_key,path_to_image);

Add Image to the Stage

this.char1=game.add.sprite(x,y,library_key);

Preload Sprite Sheet

game.load.spritesheet('ref_name', 'pathto.png', sprite_width, sprite_height, number_of_cells);

Set Sprite Top Position

this.char1.y=top_postion;

##

Set the Left Position of a Sprite


Set the registration point for a sprite

this.char1.x=left_position;

##

Add an animation to a sprite

mysprite.animations.add('walk', frame_array, frames_sec,loopBoolean);

##

Play an animation

mysprite.animations.play('animation name');

##

Add a mask to a sprite

// A mask is a Graphics object
mask = game.add.graphics(0, 0);
// Shapes drawn to the Graphics object must be filled.
mask.beginFill(fill_color);
// Here we'll draw a circle
mask.drawCircle(left, top, radius);
// And apply it to the Sprite
sprite.mask = mask;

##

Set a sprite frame

sprite.frame=frame_number;

##

Add a group

var myGroup=game.add.group();
myGroup.add(sprite);

##

Add a Tilesprite

this.tileSprite = game.add.tileSprite(x, y, width, height, 'key');

Prototype template

var Monster = function(x=0, y=0, frame=0) {
    Phaser.Sprite.call(this, game, x, y, 'monster', frame);
    // initialize your prefab here
};
Monster.prototype = Object.create(Phaser.Sprite.prototype);
Monster.prototype.constructor = Monster;
Monster.prototype.update = function() {
    // write your prefab’s specific update code here
};

Create sprite in group

group.create(x, y,'key');

##

Loop through group

myGroup.forEach(function(item) {         
}.bind(this));

##

Create multiple

myGroup.createMultiple(20, 'myKey', 0, false);

Detect the end of a animation

myAnimation.onComplete.add(function, scope);

##

Load a sprite sheet with json

game.load.atlasJSONHash('letters',"images/main/letters.png","images/main/letters.json");