Mouse does not work!

I’ve been working with Monogame the past three years, and it is great. But I’ve had some difficult with mouse position. It used to work, but if I use classes, it doesn’t work. This is my test project:
Game1 class:

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Playerclass Player;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }
    protected override void Initialize()
    {
        this.IsMouseVisible = true;
        base.Initialize();
    }
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        Player = new Playerclass();
        Player.LoadContent(Content);
    }
    protected override void UnloadContent()
    {
    }
    protected override void Update(GameTime gameTime)
    {
        Player.Update(gameTime);
        base.Update(gameTime);
    }
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        //spriteBatch.Draw(Guy, Mouse.GetState().Position.ToVector2(), Color.White);
        Player.Draw(spriteBatch);
        spriteBatch.End();
        base.Draw(gameTime);
    }

Player class:

    Texture2D Guy;
    Point Position;
    public void LoadContent(ContentManager Content)
    {
        Guy = Content.Load<Texture2D>("Guy");
    }
    public void Update(GameTime gameTime)
    {
        Position = Mouse.GetState().Position;
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(Guy, Position.ToVector2(), Color.White);
    }

It just returns 0,0! What is the problem? Please help!

It might just be me, but that code doesn’t make sense. In your Game class you have

Player.Update(gameTime, Mouse.GetState().Position.ToVector2()); // notice the 2 arguments

but then inside your Player class you have

public void Update(GameTime gameTime) // notice there is only 1 parameter

There is something funky going on here… something you’re not sharing with us.

Whoops! Sorry, wrong source code!

I can’t see any issues with that. Maybe try zipping up the project, and sharing that.

Ok, I can send you my project!

Here you go: https://mega.nz/#!Rjp3nSBa!T-j59sq5Yeg1xSv1dxqMcZJGck7hnDDtZaqIHp8bAEY

Mouse position just returns 0,0. And leftbutton is always false. I don’t know if it is just with my computer, if so, I don’t know the problem. I have installed monogame over and over again, but It still doesn’t work. It has been happening for one year.

Oh boy. You’re just a silly sausage @anon88365101.

You’re “Playerclass” is inheriting “Game1”.

public class Playerclass : Game1

Because of that, you’d need to “override” your Update method in the “Playerclass” class.

But that’s not the point, you shouldn’t have a player inheriting from Game1. It just doesn’t make sense.

When you inherit think of the words “is a”. So:
Player is a Game1 - no. No it’s not.

Also you should rename your class from “Playerclass” to “Player”.

Just to be clear. You need:

public class Playerclass

Not:

public class Playerclass : Game1
2 Likes

OMG! Thank you so, so much! Thank you! I used to inherit Game1 because of some public variables! Do you know another way to share public variables? Anyway, thank you so, so much! You are a lifesaver!

Try passing Game1 through the constructor.

Playerclass.cs:

private Game1 _game1;
public Player(Game1 game1)
{
  _game1 = game1;
}

Game1.cs:

Player = new Playerclass(this);

You should then be able to use the the _game1 field to access those properties.

Thank you again! You’ve helped me a lot!

Would now be a good time to ask you to become a Patreon? :wink:

Nah, I’m joking. Happy to help! :slight_smile:

Wait a minute. You are this guy: https://www.youtube.com/channel/UCEXxtHhSvUmz2qcZoWay9kw

1 Like

Yes - that guy and I are the same.