【问题标题】:Increasing number once per mouse click/key press in XNA在 XNA 中,每次鼠标单击/按键都会增加一次数字
【发布时间】:2010-03-07 03:51:29
【问题描述】:

这一直困扰着我——当我使用下面的代码来增加每次鼠标点击的选择时: if (m.LeftButton == ButtonState.Pressed) currentSelection++; 然后 currentSelection 增加了一吨,仅仅是因为这段代码在我的 Update() 函数中,并且按照设计,运行每一帧,因此增加了 currentSelection。您几乎不可能以足够快的速度单击和释放以防止 currentSelection 增加超过一。

现在我的问题是我应该怎么做才能做到这一点,所以每次我点击鼠标一次,它只会增加一次 currentSelection 直到我下次再次点击。

【问题讨论】:

    标签: c# mouse xna


    【解决方案1】:

    您需要比较当前鼠标状态和上次更新的鼠标状态。

    在您的班级中,您将声明 MouseState mouseStateCurrent, mouseStatePrevious;,所以它会是这样的:

    mouseStateCurrent = Mouse.GetState();
    
    if (mouseStateCurrent.LeftButton == ButtonState.Pressed &&
        mouseStatePrevious.LeftButton == ButtonState.Released)
    {
        currentSelection++;
    }
    
    mouseStatePrevious = mouseStateCurrent;
    

    所以它会检测到你上次按下它,然后你松开它——只有这样它才会被认为是一次点击,它会添加到currentSelection

    【讨论】:

    • 我收到了这个奇怪的错误,它说 && 操作数不能与 ButtonState 一起使用
    • 哈哈,没关系,我意识到你使用了'='而不是'=='。 =) 这行得通,谢谢。
    • 谢谢佝偻病!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多