Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在phaserjs中更改精灵在圆周运动中的方向_Javascript_Phaser Framework - Fatal编程技术网

Javascript 如何在phaserjs中更改精灵在圆周运动中的方向

Javascript 如何在phaserjs中更改精灵在圆周运动中的方向,javascript,phaser-framework,Javascript,Phaser Framework,我创造了一个以圆周运动的精灵。如果单击鼠标按钮(触摸),我想更改方向,但单击鼠标时,方向不会更改 这是我的代码: create:function(){ this.stage.backgroundColor='#FFFFFF'; x=this.world.centerX; y=this.world.centerY; 这个方向=1; 这是0.speedDelta=0.002; 这个半径=114; 这个.physics.startSystem(Phaser.physics.ARCADE); //添加播

我创造了一个以圆周运动的精灵。如果单击鼠标按钮(触摸),我想更改方向,但单击鼠标时,方向不会更改

这是我的代码:

create:function(){
this.stage.backgroundColor='#FFFFFF';
x=this.world.centerX;
y=this.world.centerY;
这个方向=1;
这是0.speedDelta=0.002;
这个半径=114;
这个.physics.startSystem(Phaser.physics.ARCADE);
//添加播放器
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);
},
更新:函数(){
如果(this.direction==1){
这是0.speedDelta=0.002;
}else if(this.direction==1){
这是0.speedDelta=-0.002;
}
var period=this.time.now*this.speedDelta;
this.player.x=Math.cos(句点)*this.radius;
this.player.y=d+Math.sin(句点)*this.radius;
},
changeDirection:function(){
this.direction=-this.direction;
}
}

您对
cos
sin
行为的基本假设是不正确的。你不能简单地改变输入的符号而得到不同的答案

注意:

cos(pi/4) = 0.707
cos(-pi/4) = 0.707

sin(pi/4) = -0.707
sin(-pi/4) = -0.707
另外,我认为您的代码将受益于使用稍微不同的方法

目前,您正在每个更新周期从头开始重新计算位置。为了获得您想要的行为,我认为更简单的方法是根据速度和方向计算位置增量,然后简单地将增量添加到当前位置


这也将允许您删除条件语句,这将使代码更干净。

您检查了两次
方向==1
。感谢您的回复,我将第二次检查更改为方向==-1,但仍然不起作用。