c# - 如何检查鼠标左键是否双击?

标签 c# xna xna-4.0

在我做的构造函数中:

mouseState = Mouse.GetState();
var mousePosition = new Point(mouseState.X, mouseState.Y);

然后在我创建的输入法中添加:

private void ProcessInput(float amount)
{
     Vector3 moveVector = new Vector3(0, 0, 0);
     KeyboardState keyState = Keyboard.GetState();
     if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.W))
         moveVector += new Vector3(0, 0, -1);
     if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.S))
         moveVector += new Vector3(0, 0, 1);
     if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.D))
         moveVector += new Vector3(1, 0, 0);
     if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.A))
         moveVector += new Vector3(-1, 0, 0);
     if (keyState.IsKeyDown(Keys.Q))
         moveVector += new Vector3(0, 1, 0);
     if (keyState.IsKeyDown(Keys.Z))
         moveVector += new Vector3(0, -1, 0);
     if (keyState.IsKeyDown(Keys.Escape))
     {
         this.graphics.PreferredBackBufferWidth = 800;
         this.graphics.PreferredBackBufferHeight = 600;
         this.graphics.IsFullScreen = false;
         this.graphics.ApplyChanges();
     }
     if (mouseState.LeftButton == ButtonState.Pressed)
     {
         this.graphics.PreferredBackBufferWidth = 1920;
         this.graphics.PreferredBackBufferHeight = 1080;
         this.graphics.IsFullScreen = true;
         this.graphics.ApplyChanges();
     }

     AddToCameraPosition(moveVector * amount);
 }

我补充说:

if (mouseState.LeftButton == ButtonState.Pressed)
{
    this.graphics.PreferredBackBufferWidth = 1920;
    this.graphics.PreferredBackBufferHeight = 1080;
    this.graphics.IsFullScreen = true;
    this.graphics.ApplyChanges();
}

我使用了一个断点,当点击鼠标左键时它没有任何反应。 它永远不会进入这个 if block 。 它正在到达 if 但从未进入它。

那么我该如何让它发挥作用呢? 以及如何同时双击鼠标左键而不是单击鼠标左键?

最佳答案

首先,你必须打电话

mouseState = Mouse.GetState()

在每个 Update() 周期中。对于您来说,这可能是在 ProcessInput 方法的开头。

其次,即便如此,您编写的代码也无法运行。你的代码,现在,几乎说“只要按下左键,将游戏切换到全屏”。 XNA 不是事件驱动的 - 没有 OnClick 或 OnDoubleClick 事件,您必须自己实现这些事件或使用可用的属性。

您可能想要实现这样的功能:

MouseState previousState;
MouseState currentState;
bool WasMouseLeftClick()
{
    return (previousState.LeftButton == ButtonState.Pressed) && (currentState.LeftButton == ButtonState.Released);
}

然后,在您的 ProcessInput 函数中,将其添加到开头:

previousState = currentState;
currentState = Mouse.GetState();

你可以使用它:

if (WasMouseLeftClick())
{
    // Switch to fullscreen.
}

添加对双击使用react的功能会稍微复杂一些。您将必须定义点击之间允许的最大延迟。然后,每个循环,如果你有点击,你将需要检查最后一次点击发生的时间。如果它小于之前的延迟,我们会双击。像这样:

const float MAXDELAY = 0.5f; // seconds
DateTime previousClick;
bool WasDoubleClick()
{
   return WasMouseLeftClick() // We have at least one click, and
       && (DateTime.Now - previousClick).TotalSeconds < MAXDELAY;
}

此外,您还需要将此添加到 ProcessInput 的末尾:(请注意,您必须仅在检查双击后添加此内容,否则它将把所有点击都解释为双击)

if (WasMouseLeftClick())
{
  previousClick = DateTime.Now;
}

关于c# - 如何检查鼠标左键是否双击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19480228/

相关文章:

c# - 如何使变量对 XNA/monogame 中的所有类可用?

XNA 游戏组件

c# - XNA 二维视野

c# - 使用 union 从 C# 调用 C++ DLL

c# - 如何编码 cvMat 和 CV_MAT_ELEM

c# - Dynamodb putitem 功能无法正常工作

c# - 找不到引用 Microsoft.DirectX.AudioVideoPlayback 和 Microsoft.DirectX

XNA SoundEffect 无法从文件加载

c# - Sprite 变得模糊

c# - 正则表达式不能在土耳其语字符上正常工作