0

My monogame game has stopped responding to mouse clicks. Prior to version 3.5, this was working fine. Here's how I'm currently getting the input:

protected override void Update (GameTime game_time)
  {
  Mouse_Input (game_time);
  }

void Mouse_Input(GameTime game_time)
  {
  mouse_current = Mouse.GetState();

  if (mouse_current.LeftButton == ButtonState.Pressed)
    {
    // click
    }
  }

Setting breakpoints in the function reveals all the code is being hit, but LeftButton is always ButtonState.Released.

I've tried with both a wired mouse and the trackpad. Keyboard input is working fine. Anyone else running into this?

Nightmare Games
  • 2,205
  • 6
  • 28
  • 46

2 Answers2

0

I always use this way.

MouseState currentMouseState;
MouseState oldMouseState;

public bool checkClick()
{
    oldMouseState = currentMouseState;
    currentMouseState = Mouse.GetState();

    if (Visible)
    {
        if (currentMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released)
        {
            return true;
        }
    }
}

If you want to check if the Mouse clicks on a Rectangle (Hud elements for example)

public bool checkClickRectangle(Rectangle rec)
{
    oldMouseState = currentMouseState;
    currentMouseState = Mouse.GetState();

    if (Visible)
    {
        if (rec.Contains(new Vector2(Mouse.GetState().X, Mouse.GetState().Y)) && currentMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released)
        {
            return true;
        }
    }
}
SKSK
  • 13
  • 4
0

This was actually a not a problem with Monogame, but a problem in my game logic that was very difficult to track down.

After upgrading to 3.5, I had to reconfigure how my Texture2D's were being loaded, which also meant refactoring some classes. I ended up with a class within a class which were both inheriting from Game.

public class Brush_Control : Game
  {
  public class Tile : Game
    {

Process of elimination narrowed the search to this class. I believe this caused an infinite loop that interfered with the input somehow, but without throwing an error or causing an obvious freeze.

Removing the inner Game reference as a parent fixed the problem, and it turns out I no longer need to have it there anyway.

Nightmare Games
  • 2,205
  • 6
  • 28
  • 46