开发者

Increasing number once per mouse click/key press in XNA

This has been bothering me lately- when I use some code like below to increase selection every mouse click: if (m.LeftButton == ButtonState.Pressed) currentSelection++; Then currentSelection increases by a ton, simply because this code is in my Update() function and by design, runs every frame and so increases currentSelection. There is almost no chance you can click and release fast enough to prevent currentSelection from increasing more than one.

Now my question is what I should do to make it so everytime I click the mouse down once, it only increases currentSelection once until the next开发者_开发技巧 time I click down again.


You will need to compare the current mouse state and the last update's mouse state.

In your class you'll have MouseState mouseStateCurrent, mouseStatePrevious; declared and so it'll be something like:

mouseStateCurrent = Mouse.GetState();

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

mouseStatePrevious = mouseStateCurrent;

So it will detect that previous time you pressed it, then you release it - only then it is considered as a click and it will add to currentSelection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜