Useful Snippets

Below are some useful Code Snippets that I use for my games

PAUSING A GAME

//create a pause button
this.pauseButton = this.add.button(this.game.width /2 ,10, 'pause');

this.pauseButton.events.onInputDown.add(function(){
this.game.paused = true;

//add a message when paused
this.message = this.game.add.text(this.game.width / 2,100, 'GAME PAUSED, CLICK ANYWHERE TO RESUME');
}, this);
// Add a input listener that can help us return from being paused
this.game.input.onDown.add(function(){

if(this.game.paused){
this.game.paused = false;
this.message.visible = false;
}
}, this);

Rotation Tween

var playerRotation = this.game.add.tween(this.player);
//make the player sprite do two loops in one second
playerRotation.to({angle: '+720'}, 1000);

playerRotation.onComplete.add(function(){
// do something here after completion
}, this);

playerRotation.start();

Quick Emitter

var emitter = this.game.add.emitter(this.player.x, this.player.y, 15);
emitter.makeParticles('star');
emitter.minParticleSpeed.setTo(-300, -300);
emitter.maxParticleSpeed.setTo(300, 300);
emitter.gravity = 0;
emitter.start(true, 750, null, 15);