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?