Mouse input doesn't work.

Mouse.GetState() doesn’t update. I use DirectX and monogame 3.8.

I can’t find anything on google…

EDIT: my sprite class doesn’t allow mouse to update. I don’t know why.

Elaborate on “doesn’t work”.

1 Like

Mouse position doesn’t update

EDIT: my sprite class doesn’t allow mouse to update. I don’t know why.

maybe you should share some of your code?

Maybe it’s not very easy to understand…

Game1.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.IO;

namespace PirateOcean
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;

    private string gameloadinput;

    private string play = "menu";

    private Texture2D title;
    private Texture2D titleobject;

    private Water water;

    private int mouseX;
    private int mouseY;

    public Game1()
    {
        _graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        IsMouseVisible = true;

        _graphics.IsFullScreen = true;

        _graphics.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
        _graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;


        _graphics.ApplyChanges();
    }

    protected override void Initialize()
    {

        base.Initialize();
        Mouse.SetPosition(0, 0);
    }

    protected override void LoadContent()
    {
        _spriteBatch = new SpriteBatch(GraphicsDevice);

        
        water = new Water(Content.Load<Texture2D>("water1"));

        title = Content.Load<Texture2D>("Title");
        titleobject = Content.Load<Texture2D>("titleobject");

    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        MouseState mouseState = Mouse.GetState();
        mouseX = mouseState.X;
        mouseY = mouseState.Y;

        base.Update(gameTime);

    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.DeepSkyBlue);

     
        _spriteBatch.Begin();
        

        if (play == "menu") 
        {
            Menu();
        }

        _spriteBatch.End();

        base.Draw(gameTime);
    }

    public void Menu()
    {
        _spriteBatch.Draw(titleobject, new Vector2(40, 40), Color.White);
        _spriteBatch.Draw(title, new Vector2(mouseX, mouseY), Color.White);
        water.Draw(_spriteBatch);

    }

water.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;

namespace PirateOcean
{
public class Water : Game
{

    public Vector2 waterPos = new Vector2(-1000, 0);
    public Vector2 waterPos2 = new Vector2(0, 0);
    public Vector2 waterPos3 = new Vector2(0, 0);

    Texture2D texture;
    int count = 0;


    int position = 0;
    int[] Yposition = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
    int[] Yposition2 = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28};


    public Water(Texture2D texture)
    {
        this.texture = texture;
    }


    public void WaterMove()
    {

        //y position

        if (count == 15 && position == 0)
        {
            count = 14;
            position = 1;
        }
        if (count == 0 && position == 1)
        {
            count = 0;
            position = 0;
        }

        //change pos
        waterPos.Y = Yposition[count] + 640;
        waterPos2.Y = Yposition[count] + 650;
        waterPos3.Y = Yposition[count] + 660;

        if (position == 0)
        {
            count += 1;
        }
        else
        {
            count -= 1;
        }

        //x position
        waterPos.X = 0;
    }


    public void Draw(SpriteBatch sp)
    {
        WaterMove();
        sp.Draw(texture, waterPos, null, Color.White * 0.3f);
        sp.Draw(texture, waterPos2, null, Color.White * 0.3f);
        sp.Draw(texture, waterPos3, null, Color.White * 0.3f);
    }
}

}

Okay so what’s exactly the wanted behaviour that does not work?

Why… why do you have Water inherited from the Game class? There should be only one Game instance, because it’s your whole game.

1 Like

Nevermind, i changed

public class Water : Game

to

public class Water

Thx :slight_smile: I changed

public class Water : Game

to

public class Water

I have no Idea if this is going to help or not, but you declare some private instance variables, then you make new declarations (above quote) with the same names for local variables in your Update() method. This hides the instance variables from inside Update() and you use the local variables instead. These are two separate things.
You also don’t use them in Update(), or anywhere else in the code you posted. Your IDE probably gave you a yellow squiggle stating they are unused…

Anyways, you can’t manually update the mouse state, and there is probably no point to saving copies every update.
When you call the “Mouse.GetState()” function, it supplies the real time current state of the mouse, without you having to do anything.
The variables you created are probably redundant unless you plan to change their data in some way (You will just change their value not move the mouse or anything). If you just want to find out what the current mouse position is a some point in your code, just call Mouse.GetState().X, Mouse.GetState().Y etc. no need for your own variable storage the Mouse class already stores them for you.