Liam Earle
Liam Earle

Reputation: 167

What am I doing wrong in this Monogame class?

What am I doing wrong in this class? I'm using monogame and C# but my object won't render in the program.

class Player : Game
    {
        Texture2D PlayerSprite;
        Vector2 PlayerPosition;


        public Player()
        {
            Content.RootDirectory = "Content";
            PlayerSprite = Content.Load<Texture2D>("spr_Player");
            PlayerPosition = Vector2.Zero;
        }

        public void Update()
        {


        }

        public void Draw(SpriteBatch SpriteBatch)
        {
            SpriteBatch.Begin();
            SpriteBatch.Draw(PlayerSprite,PlayerPosition, new Rectangle(0, 0, 32,32), Color.White);
            SpriteBatch.End();
        }
    }

Upvotes: 0

Views: 88

Answers (1)

Willians Monteiro
Willians Monteiro

Reputation: 26

  • The Load, Update, and Draw methods belong to the inherited Game class and are overrided by it.
  • You too need to start your SpriteBacth object.
  • GraphicsDevice object has exist in Game main class.

Try this:

class Player : Game
{
    Texture2D PlayerSprite;
    Vector2 PlayerPosition;
    SpriteBatch spriteBatch;

    public Player()
    {
        Content.RootDirectory = "Content";
        PlayerSprite = Content.Load<Texture2D>("spr_Player");
        PlayerPosition = Vector2.Zero;
    }

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

    protected override void Update(GameTime gameTime)
    {
    }

    protected override void Draw(GameTime gameTime)
    {
        spriteBatch.Begin();
        spriteBatch.Draw(PlayerSprite,PlayerPosition, new Rectangle(0, 0, 32,32), Color.White);
        spriteBatch.End();
    }
}

Upvotes: 1

Related Questions