Phaser current version test environment
Basic Phaser setup for testing
by Lewis Lane
HTML
<script src="https://rawgit.com/photonstorm/phaser/master/build/phaser.min.js"></script>
<div id="test"></div>
JavaScript
// Phaser current version test environment
// Moving away from pointer
var BasicGame = {
game: new Phaser.Game(500, 400, Phaser.AUTO, 'test')
};
var reactionDistance = 150, angle, distance;
BasicGame.Main = {
create: function (game) {
this.circleBmd = game.add.bitmapData(32, 32);
this.circleBmd.circle(16, 16, 16, '#fff');
this.circle = game.add.sprite(game.world.centerX, game.world.centerY, this.circleBmd);
this.circle.anchor.set(0.5);
game.physics.arcade.enable(this.circle);
this.circle.body.collideWorldBounds = true;
},
update: function (game) {
// Get the angle (in radians)
var angle = game.physics.arcade.angleBetween(this.circle, game.input);
// Convert the angle to degrees and then reverse the angle
angle = angle * (180 / Math.PI) - 180;
// Get the distance
var distance = game.physics.arcade.distanceBetween(this.circle, game.input);
// Calculate the velocity, higher if closer
var velocity = Math.max(0, reactionDistance - distance);
// Apply the angle and velocity to the sprite
game.physics.arcade.velocityFromAngle(angle, velocity, this.circle.body.velocity);
}
};
BasicGame.game.state.add('Main', BasicGame.Main);
BasicGame.game.state.start('Main');