【问题标题】:Touch button for rotate in Phaser在 Phaser 中旋转的触摸按钮
【发布时间】:2015-09-24 14:31:33
【问题描述】:

我一直在为 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;
  }
}

有人知道为什么它会像那样限制顺时针旋转吗?

非常感谢。

【问题讨论】:

    标签: javascript mobile rotation touch phaser-framework


    【解决方案1】:

    Phaser 有一个名为“角度”的精灵属性来改变旋转方向。 Here is their example。尽量只使用 Phaser 中的属性,以避免遇到框架外的任何侥幸。

    【讨论】:

      【解决方案2】:

      这是因为您的if 逻辑错误。当您按下 rightArrowLeftIsPressed 时,您正在顺时针旋转汽车 - 但是,您也没有按下 leftArrowLeftIsPressed,它正在执行您的 else 块,它将汽车捕捉到最近的 90 度。将您的代码更改为以下内容:

      ....
      // 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;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多