我一直在为Phaser游戏苦苦挣扎。我一直试图制作我的游戏控制的移动版本,但是出于合理的原因,在达到0度,90度,180度或270度后,顺时针旋转我的汽车角色。逆时针工作正常。
这就是它的样子:
//I made a turnSpeed dependant on the car's current speed
turnSpeed = speed / 6000;
//I then check for a boolean which is true if the button is touched
if (rightArrowLeftIsPressed)
{
playerCar.rotation += turnSpeed;
}
if (leftArrowLeftIsPressed)
{
playerCar.rotation -= turnSpeed;
}
//This will snap the car to 0, 90, 180 or 270 degrees if no button is pressed and the car is close to that rotation already
else
{
if (playerCar.rotation < 0.15 && playerCar.rotation > -0.15)
{
playerCar.rotation = 0;
}
if (playerCar.rotation > (Math.PI - 0.15) || playerCar.rotation < (-Math.PI + 0.15))
{
playerCar.rotation = Math.PI;
}
if (playerCar.rotation > -Math.PI / 2 - 0.15 && playerCar.rotation < -Math.PI / 2 + 0.15)
{
playerCar.rotation = -Math.PI / 2;
}
if (playerCar.rotation > Math.PI / 2 - 0.15 && playerCar.rotation < Math.PI / 2 + 0.15)
{
playerCar.rotation = Math.PI / 2;
}
}
任何人都知道为什么会像这样限制顺时针旋转?
非常感谢。
答案 0 :(得分:0)
Phaser有一个叫做“角度”的精灵属性来改变旋转方向。 Here is their example。尽量只使用Phaser中的属性,以避免在框架外遇到任何吸虫。
答案 1 :(得分:0)
这是因为你的if
逻辑错了。当您按rightArrowLeftIsPressed
时,您正在顺时针旋转汽车 - 但是,您也没有按leftArrowLeftIsPressed
正在执行else
挡住车辆的....
// this will check to see if either button is pressed, if so rotate the car, if not, snap the car
if (rightArrowLeftIsPressed || leftArrowLeftIsPressed) {
if (rightArrowLeftIsPressed) playerCar.rotation += turnSpeed;
if (leftArrowLeftIsPressed) playerCar.rotation -= turnSpeed;
}
//This will snap the car to 0, 90, 180 or 270 degrees if no button is pressed and the car is close to that rotation already
else
{
if (playerCar.rotation < 0.15 && playerCar.rotation > -0.15)
{
playerCar.rotation = 0;
}
if (playerCar.rotation > (Math.PI - 0.15) || playerCar.rotation < (-Math.PI + 0.15))
{
playerCar.rotation = Math.PI;
}
if (playerCar.rotation > -Math.PI / 2 - 0.15 && playerCar.rotation < -Math.PI / 2 + 0.15)
{
playerCar.rotation = -Math.PI / 2;
}
if (playerCar.rotation > Math.PI / 2 - 0.15 && playerCar.rotation < Math.PI / 2 + 0.15)
{
playerCar.rotation = Math.PI / 2;
}
}
到最接近的90度。将您的代码更改为:
@Override
public String toString() {
// Create a copy, don't share the array
return new String(value, 0, count);
}