Jump to content

change direction of sprite in circular motion


kemz
 Share

Recommended Posts

I created a sprite which move in a circular motion . I want to change the direction if the mouse button is clicked, but it's working.Below is my code. Any help pls. thanks in advance.

create: function () {     	this.stage.backgroundColor = '#FFFFFF' ;        x =  this.world.centerX;        y =this.world.centerY;               this.direction = 1;        this.speedDelta= 0.002;        this.radius = 114;        this.physics.startSystem(Phaser.Physics.ARCADE);                              //adding player        this.player = this.add.sprite( x  , y ,'player');        this.player.anchor.setTo(0.5, 0.5);        this.game.physics.arcade.enable(this.player);                               this.input.onDown.add(this.changeDirection, this);    },    update: function(){        var period = this.time.now *  this.speedDelta;             this.player.x =   this.direction * Math.cos(period) * this.radius;      this.player.y =   this.direction * (d+Math.sin(period) * this.radius);                   },    changeDirection: function(){               this.direction =  -this.direction;}}
Link to comment
Share on other sites

Thanks for your replies. It is still not working. The direction doesn't change.  Below is my second attempt:

create: function () {     	this.stage.backgroundColor = '#FFFFFF' ;        x =  this.world.centerX;        y =this.world.centerY;               this.direction = 1;        this.speedDelta= 0.002;        this.radius = 114;        this.physics.startSystem(Phaser.Physics.ARCADE);                              //adding player        this.player = this.add.sprite( x  , y ,'player');        this.player.anchor.setTo(0.5, 0.5);        this.game.physics.arcade.enable(this.player);                               this.input.onDown.add(this.changeDirection, this);    },    update: function(){     if(this.direction == 1){     this.speedDelta  = 0.002;}else if(this.direction == 1){ this.speedDelta = -0.002;}        var period = this.time.now *  this.speedDelta;             this.player.x =   this.direction * Math.cos(period) * this.radius;      this.player.y =   this.direction * (d+Math.sin(period) * this.radius);                   },    changeDirection: function(){               this.direction =  -this.direction;}}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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