Phaser Screen Shake
HTML
<script src="https://rawgithub.com/photonstorm/phaser/dev/build/phaser.js"></script>
JavaScript
window.onload = function () {
Phaser.Plugin.ScreenShake = function (game, parent) {
Phaser.Plugin.call(this, game, parent);
this.screenShakes = 0;
this.shakedAt = 0;
};
Phaser.Plugin.ScreenShake.prototype = Object.create(Phaser.Plugin.prototype);
Phaser.Plugin.ScreenShake.prototype.constructor = Phaser.Plugin.ScreenShake;
Phaser.Plugin.ScreenShake.prototype.start = function (count) {
if(this.game.time.now - this.shakedAt < 200) {
return;
}
this.shakedAt = this.game.time.now;
this.screenShakes = count;
if (window.navigator && window.navigator.vibrate) {
navigator.vibrate(count*10);
}
};
Phaser.Plugin.ScreenShake.prototype.postUpdate = function () {
if (this.screenShakes > 0) {
this.screenShakes--;
var amt = this.screenShakes * 0.5;
if (this.screenShakes % 2) {
this.game.camera.y += amt;
}
else {
this.game.camera.y -= amt;
}
this.game.camera.displayObject.position.y = -this.game.camera.view.y;
}
};
var sprite,
isGrounded = false,
game = new Phaser.Game(640, 360, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
function preload() {
game.load.crossOrigin = 'anonymous';
game.load.image('bg', 'http://placekitten.com/g/840/560');
game.load.image('cat', 'http://placekitten.com/g/80/80');
}
function create() {
var screenShake = game.plugins.add(Phaser.Plugin.ScreenShake);
game.plugins.screenShake = screenShake;
game.input.keyboard.addKeyCapture([
Phaser.Keyboard.SPACEBAR,
Phaser.Keyboard.UP,
Phaser.Keyboard.DOWN
]);
game.physics.startSystem(Phaser.Physics.ARCADE);
game.physics.arcade.gravity.y = 1000;
game.world.setBounds(-100, -100, 840, 560);
game.add.image(-100, -100, 'bg');
sprite = game.add.sprite(80, 0, 'cat');
game.physics.enable(sprite, Phaser.Physics.ARCADE);
sprite.body.allowRotation...