Skip to content

Instantly share code, notes, and snippets.

@cocoademon
Last active December 27, 2015 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cocoademon/7276093 to your computer and use it in GitHub Desktop.
Save cocoademon/7276093 to your computer and use it in GitHub Desktop.
Simple "Shake" plugin for Phaser 1.1.3 with postUpdate
/* Simple "Shake" plugin */
/* Use it like: */
var shake = new Phaser.Plugin.Shake(game);
game.plugins.add(shake);
shake.shake(40); // 40 pixels max displacement
/*
Plugin implementation
*/
Phaser.Plugin.Shake = function(game, parent) {
Phaser.Plugin.call(this, game, parent);
this.offsetX = 0;
this.offsetY = 0;
this.size = 20;
this.amt = 0.0;
this.cache = 0;
};
Phaser.Plugin.Shake.prototype = Object.create(Phaser.Plugin.prototype);
Phaser.Plugin.Shake.prototype.postUpdate = function () {
this.cache = this.amt * this.size;
this.offsetX = ((Math.random() * 2 - 1) * this.cache)|0;
this.offsetY = ((Math.random() * 2 - 1) * this.cache)|0;
this.game.camera.displayObject.position.x += this.offsetX;
this.game.camera.displayObject.position.y += this.offsetY;
this.amt *= 0.95; // Todo: framerate independence!
};
Phaser.Plugin.Shake.prototype.postRender = function() {
this.game.camera.displayObject.position.x -= this.offsetX;
this.game.camera.displayObject.position.y -= this.offsetY;
}
Phaser.Plugin.Shake.prototype.shake = function (size) {
if(typeof(size) !== 'undefined') {
this.size = size;
}
this.amt = 1.0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment