Jump to content

Circular Motion with Controls


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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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