Jump to content

Circular Motion with Controls


0penS0urce
 Share

Recommended Posts

I need a code snippet to allow a sprite to orbit the center of the screen, and the direction has to be controlled with the arrow keys, and it has to rotate to face the middle. The code I have right now flips the sprite to the other side of the circle. Thanks in Advance!

var game = new Phaser.Game(640, 480, Phaser.AUTO, '', { preload: preload, create: create, update: update });function preload() {    game.load.image('player', 'assets/player.png');}function create() {    game.physics.startSystem(Phaser.Physics.ARCADE);    x = game.add.sprite(0, 0, 'player');    game.physics.enable(x, Phaser.Physics.ARCADE);    x.anchor.setTo(0.5, 0.5);    cursors = game.input.keyboard.createCursorKeys();}function update() {    var period = game.time.now * 0.001;    var radius = 200;    if (cursors.left.isDown)    {        x.x = game.world.centerX - Math.cos(period) * radius;        x.y = game.world.centerY - Math.sin(period) * radius;        x.angle += 1;        }    if (cursors.right.isDown)    {        x.x = game.world.centerX + Math.sin(period) * radius;        x.y = game.world.centerY + Math.cos(period) * radius;        x.angle -= 1;    }    }
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...