-2

i am new to monogame and don't really understand it very well.

I keep getting these errors ever since i'm trying to implement a camera which follows a sprite:

'Sprite' does not implement inherited abstract member 'Component.Update(GameTime)'

'Sprite' does not implement inherited abstract member 'Component.Draw(GameTime, SpriteBatch)'

Sprite Class:

public class Sprite : Component
{
    #region Fields
    public string name;

    public Sprite()
    {

    }

    protected AnimationManager _animationManager;

    protected Dictionary<string, Animation> _animations;

    protected Vector2 _position;

    protected Texture2D _texture;

    public Rectangle Rectangle
    {
        get { return new Rectangle((int)Position.X, (int)Position.Y, _texture.Width, _texture.Height); }
    }

    #endregion

    #region Properties

    public Input Input;

    public Vector2 Position
    {
        get { return _position; }
        set
        {
            _position = value;

            if (_animationManager != null)
                _animationManager.Position = _position;
        }
    }

    public float Speed = 5f;

    public Vector2 Velocity;

    #endregion

    #region Methods

    public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        if (_texture != null)
            spriteBatch.Draw(_texture, Position, Color.White); //draws the texture if it has a value
        else if (_animationManager != null)
            _animationManager.Draw(spriteBatch);//draws animation if it has an animation
        else throw new Exception("animation error");
    }

    public virtual void Move()
    {
        if (Keyboard.GetState().IsKeyDown(Input.Up))
            Velocity.Y = -Speed;
        if (Keyboard.GetState().IsKeyDown(Input.Down))
            Velocity.Y = Speed;
        if (Keyboard.GetState().IsKeyDown(Input.Left))
            Velocity.X = -Speed;
        if (Keyboard.GetState().IsKeyDown(Input.Right))
            Velocity.X = Speed;
        if (Keyboard.GetState().IsKeyDown(Input.Shift))
            Speed = 7.5f;
        else
            Speed = 5f;

    }

    protected virtual void SetAnimations()
    {
        if (Velocity.X > 0)
            _animationManager.Play(_animations["WalkRight"]);
        else if (Velocity.X < 0)
            _animationManager.Play(_animations["WalkLeft"]);
        else if (Velocity.Y > 0)
            _animationManager.Play(_animations["WalkDown"]);
        else if (Velocity.Y < 0)
            _animationManager.Play(_animations["WalkUp"]);

        else _animationManager.Stop();
    }

    public Sprite(Dictionary<string, Animation> animations)
    {
        _animations = animations;
        _animationManager = new AnimationManager(_animations.First().Value);
    }

    public Sprite(Texture2D texture)
    {
        _texture = texture;
    }

    public virtual void Update(GameTime gameTime, List<Sprite> sprites)
    {
        Move();

        SetAnimations();

        _animationManager.Update(gameTime);

        Position += Velocity;
        Velocity = Vector2.Zero;
    }
    //public override void Update(GameTime gameTime)
    //{

    //}
    #endregion
}

Component Class:

public abstract class Component
{
    public abstract void Draw(GameTime gameTime, SpriteBatch spriteBatch);

    public abstract void Update(GameTime gameTime);
}

I played around fixing this error and realised changing the Draw method to Override instead of virtual and adding an Update override method. But then this made another error on line 33:

public Rectangle Rectangle
    {
        get { return new Rectangle((int)Position.X, (int)Position.Y, _texture.Width, _texture.Height); }
    }

Error: System.NullReferenceException: 'Object reference not set to an instance of an object.'

_texture was null.

Thank you, sorry for long post, any ideas how to fix this?

  • It sounds like you fixed your original issue. Your NullReferenceException issue is completely different and you'd need to ask your own question if you get stuck. But warning, you need to read [this](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) first, otherwise your question will likely get closed as a duplicate rather quickly. – mason Mar 19 '20 at 19:49

1 Answers1

0

You have to remove virtual from both the offending methods Draw and Update and mark them with override as you did.

The only issue why you were getting NullReferenceException is that your _texture field is null. For that, you will have to make sure that _texture field gets instantiated in the constructor that you are using to construct the Sprite instance.

If all this is too difficult to comprehend for you right now, I suggest you first go over the basics of C#. You will find multiple resources on MSDN and on youtube.

Rajan Prasad
  • 1,582
  • 1
  • 16
  • 33