代码之家  ›  专栏  ›  技术社区  ›  Dominic K

在XNA中,每次鼠标点击/按键增加一次次数

  •  4
  • Dominic K  · 技术社区  · 16 年前

    这最近一直困扰着我-当我使用下面的代码来增加每次鼠标点击的选择时: if (m.LeftButton == ButtonState.Pressed) currentSelection++; 然后currentSelection增加了一吨,因为这段代码在我的update()函数中,并且按设计运行了每一帧,所以增加了currentSelection。几乎没有可能单击并释放得足够快,以防止当前选择增加一个以上。

    现在我的问题是我应该怎么做,这样每次我向下单击鼠标时,它只会增加一次当前选择,直到下次再次向下单击。

    2 回复  |  直到 16 年前
        1
  •  6
  •   Ricket    16 年前

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

    在你的班级里 MouseState mouseStateCurrent, mouseStatePrevious; 所以它会像:

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

    所以它会检测到你上次按下它,然后你释放它-只有当它被认为是一个点击,它会添加到 currentSelection .

        2
  •  2
  •   Community Mohan Dere    9 年前