kemz Posted September 20, 2015 Share Posted September 20, 2015 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 More sharing options...
drhayes Posted September 21, 2015 Share Posted September 21, 2015 What doesn't work? When you put a "console.log('hi there');" in changeDirection, what gets printed in the dev console? Link to comment Share on other sites More sharing options...
beuleal Posted September 21, 2015 Share Posted September 21, 2015 You are changing the direction for just one frame, not for all. You should create a flag, that handle the sprite direction. Link to comment Share on other sites More sharing options...
kemz Posted September 21, 2015 Author Share Posted September 21, 2015 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 More sharing options...
Recommended Posts