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

add image to the stage
Description:Adds a sprite from the library to the stage and returns that

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

set the left position of a sprite
Description:sets the left position of a sprite

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'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 =, 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

add a group
Description:adds a group to the stage, and sprites may be added to the group;

add sprite to group
Example:var; 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

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

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

create multiple
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