C# 检测鼠标点击

C# 检测鼠标点击,c#,xna,mouseevent,C#,Xna,Mouseevent,我有一个游戏,你点击一个按钮,它会增加一个整数,然而,根据我的当前代码,用户只需按住鼠标,它就会不断增加 我怎样才能让用户只按一次(每次单击)来增加分数 以下是我的当前代码: public MouseState mouseState; protected override void Update(GameTime gameTime) { mouseState = Mouse.GetState(); if (mouseState.LeftButton == ButtonSta

我有一个游戏,你点击一个按钮,它会增加一个整数,然而,根据我的当前代码,用户只需按住鼠标,它就会不断增加

我怎样才能让用户只按一次(每次单击)来增加分数

以下是我的当前代码:

public MouseState mouseState;

protected override void Update(GameTime gameTime)
{
     mouseState = Mouse.GetState();
     if (mouseState.LeftButton == ButtonState.Pressed) 
        Managers.UserManager.OverallScore++; 

     base.Update(gameTime);
}

您可以跟踪按钮从按下状态更改为释放状态的时间,并在该时间运行您的操作,如:

bool leftButtonIsDown; // private instance field

// in your update method
if (Mouse.GetState().LeftButton == ButtonState.Pressed) {
    leftButtonIsDown = true;
} else if (leftButtonIsDown) {
    leftButtonIsDown = false;
    Managers.UserManager.OverallScore++;
}

或者当它被按下的时候再做

嗨,谢谢你的建议!我尝试了这个代码,但是这个值一直在增加,即使没有按下按钮。刷新页面,我有一个错误!非常感谢你的帮助!完美答案