Getting the position of a mouse click in a video game using C# #wpf

One of the video games being made in class needs to determine where the user clicks.  Instead of creating a MouseClick event it makes sense to incorporate this into our GameTimer_Tick event.  This is the code we came up with to test.

Code for mouse click

We added a Global Variable called Previousmbs which is the data type MouseButtonState.  We initialized it to MouseButtonState.Released.

The if statement checks if the Mouse.LeftButton was released AND whether the Previousmbs was pressed (this allows the code to only run once and not all the time the user is not pressing the button).  In my example I am removing all the items from the Canvas just to make it really visually obvious that the if statement ran.  You probably don’t want to do that!

I also have a line that modifies the title so you can see the coordinates where the mouse was clicked – you can use the coordinates to determine if there is an object on the canvas in the same location.

After the if statement is done I want to ensure that the Previousmbs is reset every time the timer ticks so that my if statement is accurate.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s