Kreij
Senior Monkey Moderator
- Joined
- Feb 6, 2007
- Messages
- 13,817 (2.07/day)
- Location
- Cheeseland (Wisconsin, USA)
When you want to update your game logic in XNA you do it in the Update method (which is overridden). This is the ideal spot to check for mouse events, like left click, right click or whatever. XNA uses a class called "Mouse" which contains a "GetState" method that returns the state of the mouse into a "MouseState" type variable.
The problem is that the Update method is called so freakin' fast (this is a good thing because it is part of your render loop) that it is almost impossible to click the mouse fast enough to get it to see only one click.
There is a pretty easy workaround for simple cases.
Create two MouseState variables.
In the Update method check for conditions in a logical "and" statement.
Basically what this will do is only run the logic when it sees that the current mouse state is different than the previous state. This prevents it from running the logic in the event you are holding down the mouse button. Since the Update method runs so fast, it appears to the program that you are holding down the button even when you are just clicking, and this prevents flickering and odd things from happening.
Do note however, this will prevent your logic from running when you are dragging the mouse. If you want to be able to drag the mouse around, the logic will have to change.
You would need to also check for some other condition to have changed. For example, the mouse has moved far enough away from it's initial position that the logic needs to be run again.
If anyone would like to see an example of something like that, just let me know.
Have fun coding!
The problem is that the Update method is called so freakin' fast (this is a good thing because it is part of your render loop) that it is almost impossible to click the mouse fast enough to get it to see only one click.
There is a pretty easy workaround for simple cases.
Create two MouseState variables.
Code:
MouseState mouseStateCurrent, mouseStatePrevious;
In the Update method check for conditions in a logical "and" statement.
Code:
[color=blue]protected override void[/color] Update([color=teal]GameTime[/color] gameTime)
{
mouseStateCurrent = [color=teal]Mouse[/color].GetState();
if (mouseStateCurrent.LeftButton = [color=teal]ButtonState[/color].Pressed &&
mouseStatePrevious.LeftButton = [color=teal]ButtonState[/color].Released)
{
// Do your mouse state logic here
}
mouseStatePrevious = mouseStateCurrent;
[color=blue]base[/color].Update(gameTime);
}
Basically what this will do is only run the logic when it sees that the current mouse state is different than the previous state. This prevents it from running the logic in the event you are holding down the mouse button. Since the Update method runs so fast, it appears to the program that you are holding down the button even when you are just clicking, and this prevents flickering and odd things from happening.
Do note however, this will prevent your logic from running when you are dragging the mouse. If you want to be able to drag the mouse around, the logic will have to change.
You would need to also check for some other condition to have changed. For example, the mouse has moved far enough away from it's initial position that the logic needs to be run again.
If anyone would like to see an example of something like that, just let me know.
Have fun coding!