Phaser Sprite Snippets

Insert image into library
Description:This will add an image to the library and make it available to your code. Tip:Use this in the preload section
Example:game.load.image("char1","images/ball.png");

add image to the stage
Description:Adds a sprite from the library to the stage and returns that
Example:this.char1=game.add.sprite(0,0,"char1");

preload sprite sheet
Description:preloads a sprite sheet to the library
Example: game.load.spritesheet('knight', 'images/knight.png', 40, 80, 6);

set sprite top position
Description:set the distance of a sprite from the top
Example:this.char1.y=400;

set the left position of a sprite
Description:sets the left position of a sprite
Example:this.char1.x=200;

set the registration point for a sprite
Description:Sets the registration point for a sprite. For example setting the anchor to 0,0 means the positioning will apply to the top left corner of the sprite setting the anchor to 0.5,0.5 will apply positioning to the middle of the sprite
Example:this.char1.anchor.setTo(0,0); this.char1.anchor.setTo(0.5,0.5);

add an animation to a sprite
Description:defines an animation for a sprite
Example:var walk=char1.animations.add('knight', [0, 1, 2, 3], 12,true);

play an animation
Description:plays an animation in a sprite that has been defined by .animations.add
Example:char1.animations.play('walk');

add a mask to a sprite
Description:adds a mask that will partly obscure a graphic
Example:// A mask is a Graphics object mask = game.add.graphics(0, 0); // Shapes drawn to the Graphics object must be filled. mask.beginFill(0xffffff); // Here we'll draw a circle mask.drawCircle(100, 100, 100); // And apply it to the Sprite sprite.mask = mask;

set sprite frame
Description:if you add a sprite to the stage that uses a sprite sheet instead of a simple image, you can select which frame is showing
Example:sprite.frame=2;

add a group
Description:adds a group to the stage, and sprites may be added to the group
Example:game.add.group();

add sprite to group
Description:
Example:var myGroup=game.add.group(); myGroup.add(sprite);

Add a Tilesprite
Description:add a sprite multiplied and stitched together. Used for scrolling backgrounds.
Example: this.tileSprite = game.add.tileSprite(0, 0, 480, 640, 'back');

prototype template
Description:use this to make a prefab/class for sprites
Example:

create sprite in group
Description:
Example:group.create(x, y,'key');

loop through group
Description:
Example:myGroup.forEach(function(item) { item.x++; }.bind(this));

create multiple
Description:
Example:myGroup.createMultiple(20, 'key', frame, alive);

detect the end of an animation
Description:calls a function when the animation is complete
Example:myAnimation.onComplete.add(this.animationDone, this);

load a sprite sheet with json
Description:loads a sprite sheet with JSON data
Example:game.load.atlasJSONHash('key',"imagePath","jsonPath);