C# 不断收到错误CS0534

C# 不断收到错误CS0534,c#,monogame,C#,Monogame,我对monogame还不熟悉,对它不是很了解 自从我尝试实现一个跟随精灵的相机以来,我一直都会遇到这些错误: “精灵”不实现继承的抽象成员“组件更新(游戏时间)” 'Sprite'不实现继承的抽象成员'Component.Draw(游戏时间,SpriteBatch) 雪碧类: public class Sprite : Component { #region Fields public string name; public Sprite() { }

我对monogame还不熟悉,对它不是很了解

自从我尝试实现一个跟随精灵的相机以来,我一直都会遇到这些错误:

“精灵”不实现继承的抽象成员“组件更新(游戏时间)”

'Sprite'不实现继承的抽象成员'Component.Draw(游戏时间,SpriteBatch)

雪碧类:

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
}
我解决了这个错误,并实现了将Draw方法改为Override而不是virtual,并添加了UpdateOverride方法。但这在第33行又犯了一个错误:

public Rectangle Rectangle
    {
        get { return new Rectangle((int)Position.X, (int)Position.Y, _texture.Width, _texture.Height); }
    }
错误: System.NullReferenceException:“对象引用未设置为对象的实例。”

\u纹理为空。


谢谢你,很抱歉发了这么长的帖子,你有什么办法可以解决这个问题吗?

你必须从有问题的方法
Draw
Update
中删除
virtual
,并像你那样用
override
标记它们

获取
NullReferenceException
的唯一原因是
\u纹理
字段是
null
。为此,您必须确保
\u texture
字段在用于构造
Sprite
实例的构造函数中实例化


如果你现在很难理解这一切,我建议你先复习C#的基础知识。您将在youtube上找到多个资源。

听起来您解决了原始问题。您的NullReferenceException问题完全不同,如果遇到问题,您需要问自己的问题。但请注意,您需要先阅读,否则您的问题很可能很快就会以重复的形式结束。
public Rectangle Rectangle
    {
        get { return new Rectangle((int)Position.X, (int)Position.Y, _texture.Width, _texture.Height); }
    }