top of page

Monogame Tutorial 1: Mouse Click.

  • Writer: Neko Nicku
    Neko Nicku
  • Feb 11, 2018
  • 1 min read

How to check if mouse has been clicked. to check the state of the mouse make a variable at the top of the file underneath this code. public class Game1 : Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; mState = Mouse.GetState(); make a boolean below that called mouseReleased and set it to true by default bool mouseReleased = true; the code Mouse.GetState contains several functions like whether a button has been pressed or released, scroll up or down, and the X / Y coordinates. to check for a mouse click go to the Update section (because we want to check every frame) if (mouseState.LeftButton == ButtonState.Pressed && mRelease == true) { //enter code in here to trigger when the mouse button is pressed. mReleased = false; } set the mouseReleased back to false after finishing the code inside the if so it doesn’t repeat. next make a new if to check if the mouse has been released and set mouse released back to true. if (mouseState.LeftButton == ButtonState.Released) { mRelease = true; } with the mRelease now being set back to true the previous code can run again if the left mouse button is pressed down.

Comments


  • Twitter Social Icon
  • Google+ Social Icon
  • YouTube Social  Icon
  • Instagram Social Icon
bottom of page