XNA键盘输入

时间:2013-04-11 18:29:56

标签: c# xna switch-statement

我在注册键盘输入时遇到了一个小错误。

       case GameState.InGame:
                {
                    **if (PlayerInput.IsKeyUp(Keys.P)&& oldInput.IsKeyDown(Keys.P))**
                         this.gameState = GameState.Paused;

                }
                   break;
            case GameState.Paused:
                {
                    **if (PlayerInput.IsKeyDown(Keys.P) && oldInput.IsKeyUp(Keys.P))**
                          this.gameState = GameState.InGame;
                }
                break;

发生的事情是它进入暂停游戏状态就好了。但是当试图从暂停状态进入InGame状态时,只要'P'键上升,它就会回到暂停状态。

2 个答案:

答案 0 :(得分:2)

在第一种情况下,您正在测试从UP到DOWN的键转换,在第二种情况下,从DOWN到UP。对两种情况使用相同的方法。

答案 1 :(得分:0)

只需在按下按钮时切换游戏状态。

if (PlayerInput.IsKeyDown(Keys.P) && oldInput.IsKeyUp(Keys.P))
{
  switch (this.gameState)
  {
    case GameState.Paused:
      this.gameState = GameState.InGame;
      break;
    case GameState.InGame:
      this.gameState = GameState.Paused;
      break;
  }
}

或者,如果您愿意:

if (PlayerInput.IsKeyDown(Keys.P) && oldInput.IsKeyUp(Keys.P))
{
  this.gameState = this.gameState == GameState.Paused ? GameState.InGame : GameState.Paused;
}