【问题标题】:Way to implement handling duration of a keypress实现按键处理持续时间的方法
【发布时间】:2023-04-09 10:53:01
【问题描述】:

我正在使用 XNA (C#) 开发 2D 平台游戏。 我想知道处理按住特定按钮的最佳方法。例如,您按住得越多,激光束就越大。

到目前为止,由于我已经在使用保存键盘最后两种状态的输入机制,因此我可以在每个更新周期进行额外检查以增加持续时间。但是,这种方法相当有限,因为它只处理一个特定的按钮(即触发按钮),它应该可以解决问题,但我想知道是否有更通用的解决方案来解决这个问题。

【问题讨论】:

  • 据我阅读 XNA KeyboardState 文档可知,KeyboardState 类没有您所描述的限制。从MSDN Documentation 来看,您应该能够使用它来获取每个键的当前状态,无论按下了多少个键,也不管它们被按下了多长时间。也许您的输入代码有问题?
  • 感谢您的检查,我自己确认了您的发现。而且我也知道输入机制,只是我在寻找更多的设计/架构解决方案。

标签: c# input xna keypress


【解决方案1】:
float ElapsedSecondsPerFrame = (float) gametime.Elapsed.TotalSeconds;

KeyFirePressedTime = Keyboard.GetState().IsKeyDown(FireKey) 
                         ? (KeyFirePressedTime + ElapsedSecondsPerFrame ) 
                         : 0;

通用方法

Dictionary<int, float> PressedKeysTime = new ...


void Update(float Elapsed)
{
      List<int> OldPressedKeys = PressedKeysTime.Keys.ToList();
      foreach (int key in Keyboard.GetState().GetPressedKeys.Cast<int>())
      {
           if (!PressedKeysTime.ContainsKey(key)) 
           {
                PressedKeysTime[key] = 0;
           } else {
               OldPressedKeys.Remove(key);
               PressedKeysTime[key] += Elapsed;
           }
      }
      foreach (int keynotpressed in OldPressedKeys) 
          PressedKeysTime.Remove(keynotpressed);

}

【讨论】:

  • 没有其他不同的方法来处理按键的持续时间,您必须在按键触发时累积时间......我不明白问题出在哪里......跨度>
  • 我更多地考虑了一个更强大的系统来处理持续时间管理。也许一些具有更通用方法的辅助类,作为参考的 3rd 方库 ...
  • 一行代码是健壮的...... :),我会寻找一个库来做一些更难的事情......我认为有一种通用的方法可以做到这一点,使用 getpressedkeys 方法...... . 但不值得面对一行代码
【解决方案2】:

这是一个简单的伪代码示例:

// to cancel immediately
if key is pressed this frame, but was released last frame
   begin effect

else if key is pressed this frame, and also was last frame
   increase magnitude of effect

else if key is released this frame and effect is active
   end effect


// or to gradually decrease
if key is pressed this frame, and effect is inactive
   begin effect

else if key is pressed this frame, and effect is active
   increase magnitude of effect

else if key is released this frame and effect is active
   decrease magnitude of effect
   if magnitude decreased enough, end effect

如你所说,具体测量持续时间是这样的:

public void Update(GameTime pGameTime)
{
   private TimeSpan mKeyDuration = TimeSpan.FromSeconds(0);

   ...

   if key is pressed this frame
      mKeyDuration += pGameTime.ElapsedGameTime;

   else if key was pressed last frame
      mKeyDuration = TimeSpan.FromSeconds(0);

   ...
}

【讨论】:

  • 是的,这是我目前正在实施的一种方式。但恐怕问题本身在于计算按键的持续时间。在您描述的场景中,我在“增加幅度”步骤中实现它。我在想是否有一些开箱即用的功能。
【解决方案3】:

检测单个按键...

lastKeyboardState = CurrentKeyState;
CurrentKeyState = Keyboard.GetState();

if ((CurrentKeyState.IsKeyUp(Keys.Enter)) && (lastKeyboardState.IsKeyDown(Keys.Enter))
        {
            //Enter was pressed
        }

检测按键被按下...

lastKeyboardState = CurrentKeyState;
CurrentKeyState = Keyboard.GetState();
if (CurrentKeyState.IsKeyDown(Keys.Enter)
        {
            //Enter was pressed (You could do BeamSize++ here
        }

这似乎是最好的方法。是你已经在使用的方法吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    相关资源
    最近更新 更多