Phaser current version test environment
Basic Phaser setup for testing
by Lewis Lane
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/phaser/2.1.0/phaser.min.js"></script>
<div id="test"></div>
CSS
body {
background: #333;
}
JavaScript
// Phaser current version test environment
// Bouncy car (simple custom physics implementation)
var game = new Phaser.Game(600, 400, Phaser.AUTO, 'test', null, false, false);
var BasicGame = function (game) {};
BasicGame.Boot = function (game) {};
var car;
BasicGame.Boot.prototype = {
preload: function () {
},
create: function () {
var carBmp = game.add.bitmapData(96, 48);
carBmp.rect(16, 0, 64, 32, '#f00');
carBmp.rect(0, 16, 96, 32, '#f00');
car = game.add.sprite(game.world.centerX, game.world.centerY, carBmp);
car.anchor.set(0.5);
// Store the velocity as a Point object
car.velocity = new Phaser.Point(0, 0);
// Bounciness - how high the car bounces back up compared to how far it fell
// 0 = no bounce, 1 = perfectly bouncy
car.bounce = new Phaser.Point(0, 0.5);
// Gravity also stored as a Point (we'll only be using y gravity)
car.gravity = new Phaser.Point(0, 20);
game.time.events.loop(1000, function() {
// Randomly jolt the car upwards every second
car.velocity.y = game.rnd.integerInRange(-1, -8);
}, this);
this.cursors = game.input.keyboard.createCursorKeys();
},
update: function () {
// Time in seconds, for framerate independent calculations
var deltaTime = game.time.elapsed * 0.001;
// Steering
if (this.cursors.left.isDown) {
car.velocity.x -= (20 * deltaTime);
}
else if (this.cursors.right.isDown) {
car.velocity.x += (20 * deltaTime);
}
else {
car.velocity.x *= (55 * deltaTime);
}
car.steer = Phaser.Math.clamp(car.velocity.x, -5, 5);
car.x += car.velocity.x;
if (car.x < car.width) {
car.velocity.x = 0;
car.x = car.width;
}
else if (car.x >...