Skip to content

Instantly share code, notes, and snippets.

@jorio
Last active November 30, 2016 15:53
Show Gist options
  • Save jorio/40e0aace6710632d8ec9b0db4064ad65 to your computer and use it in GitHub Desktop.
Save jorio/40e0aace6710632d8ec9b0db4064ad65 to your computer and use it in GitHub Desktop.
custom Shake implementation for Phaser 2 (transpiled from Shake.ts)
"use strict";
var Shake = (function () {
function Shake(game) {
this.game = game;
}
/**
* @param {number} amp amplitude in pixels
* @param {number} duration
* @param {boolean} x shake on X axis
* @param {boolean} y shake on Y axis. If x is false and y is omitted, shake on Y axis only.
*/
Shake.prototype.shake = function (amp, duration, x, y) {
this.reset();
this.x = x;
this.y = !x && y === undefined ? true : y;
this.amp = amp;
this.tween = this.game.add.tween(this);
this.tween.to({ amp: 0 }, duration, Phaser.Easing.Linear.None, true);
};
/** Internal */
Shake.prototype.reset = function () {
this.restore();
this.xoff = 0;
this.yoff = 0;
this.amp = 0;
if (this.tween) {
this.tween.stop();
}
};
/** Internal */
Shake.prototype.restore = function () {
if (this.x)
this.game.camera.x -= this.xoff;
if (this.y)
this.game.camera.y -= this.yoff;
};
/** Call this in your state update function */
Shake.prototype.update = function () {
if (!this.amp) {
return;
}
this.restore();
if (this.x) {
this.xoff = Math.round(this.game.rnd.realInRange(-this.amp, this.amp));
this.game.camera.x += this.xoff;
}
if (this.y) {
this.yoff = Math.round(this.game.rnd.realInRange(-this.amp, this.amp));
this.game.camera.y += this.yoff;
}
};
return Shake;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment