One click increase / keypress in XNA

This has bothered me lately - when I use some kind of code, as shown below, to increase the selection of each mouse click: if (m.LeftButton == ButtonState.Pressed) currentSelection++; Then the currentSelection increases by a ton, simply because this code is in my Update () function and by design, it launches every frame and therefore increases the currentSelection. There is virtually no chance that you can click and release fast enough to not increase the number of currentSelection by more than one.

Now my question is what I have to do so that each time I click on the mouse it increases the currentSelection once the next time the next time I click down again.

+4
source share
2 answers

You will need to compare the current state of the mouse and the last state of the mouse.

In your class you will have declared MouseState mouseStateCurrent, mouseStatePrevious; , and it will be something like this:

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

Thus, it will find that in the previous time you clicked it, then you release it - only then it will be considered a click, and it will add to the currentSelection .

+6
source
+2
source

Source: https://habr.com/ru/post/1303371/


All Articles