Phaser Template
Sprite Rotating But Not Moving Foward
by chongdashu
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/2.4.4/phaser.js"></script>
JavaScript
var game = new Phaser.Game(500, 340, Phaser.AUTO, '', {
preload: preload,
create: create,
update: update,
render: render
});
var button = null;
function preload() {
game.load.crossOrigin = 'anonymous';
game.load.image('player', 'http://imgur.com/oS5Q1Jx.png');
}
function create() {
game.stage.backgroundColor = '#3498db';
game.physics.startSystem(Phaser.Physics.ARCADE);
// Debug Text
text = game.add.text(
game.world.centerX,
game.world.centerY + 100, "Hello, world!", {
"fill": "white",
font: "bold 12pt Consolas",
});
text.anchor.set(0.5, 0.5)
// --
this.ROTATION_SPEED = 180;
this.ACCELERATION = 200;
this.MAX_SPEED = 250;
this.DRAG = 50;
this.player = this.game.add.sprite(this.game.width / 2, this.game.height / 2, 'player');
this.player.anchor.setTo(0.5, 0.5);
this.player.angle = 360;
this.game.physics.enable(this.player, Phaser.Physics.ARCADE);
this.player.body.maxVelocity.setTo(this.MAX_SPEED, this.MAX_SPEED);
this.player.body.drag.setTo(this.DRAG, this.DRAG);
this.game.camera.follow(this.player);
this.cursors = this.game.input.keyboard.createCursorKeys();
}
function update() {
if (this.player.x > this.game.width) this.player.x = 0;
if (this.player.x < 0) this.player.x = this.game.width;
if (this.player.y > this.game.height) player.y = 0;
if (this.player.y < 0) this.player.y = this.game.height;
if (this.cursors.left.isDown) {
this.player.body.angularVelocity = -this.ROTATION_SPEED;
} else if (this.cursors.right.isDown) {
this.player.body.angularVelocity = this.ROTATION_SPEED;
} else {
this.player.body.angularVelocity = 0;
}
if (this.cursors.up.isDown) {
this.player.body.acceleration.x = Math.cos(this.player.rotation) * this.ACCELERATION;
this.player.body.acceleration.y = Math.sin(this.player.rotation) * this.ACCELERATION;
} else if...