fedora Posted November 12, 2015 Share Posted November 12, 2015 I am attempting to create movement of a player (Ship) where holding down the left and right key will rotate the player (ship) -- and holding down the up key moves the player (ship) in the direction of said angle -- similar to http://gamemechanicexplorer.com/#thrust-2 . Unfortunately, I can get the player (ship) to rotate right and left with no problem, but when I use the up arrow nothing happens. I can only guess I am missing something obvious -- but hope that someone can spot it -- because I am missing it --- create: function() { 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(); }, update: function() { 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 (this.cursors.down.isDown) { this.player.body.acceleration.setTo(0, 0); } },Thank you in advance for the assistance. Link to comment Share on other sites More sharing options...
chongdashu Posted November 12, 2015 Share Posted November 12, 2015 This looks like a typo, which would cause an error else (this.cursors.down.isDown) { this.player.body.acceleration.setTo(0, 0); }If you change the else to an else if, things should work. Here's a fiddle: http://jsfiddle.net/chongdashu/mq8bg9sf/2/ Also, on a related note, this needs to be fixed too:if (this.player.y > this.game.height) player.y = 0; // needs to be this.player.yOtherwise, an error will occur when the player hits the bottom of the screen. Skeptron 1 Link to comment Share on other sites More sharing options...
Recommended Posts