首页 > 解决方案 > 不断收到错误 CS0534

问题描述

我是monogame的新手,不太了解它。

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

“Sprite”未实现继承的抽象成员“Component.Update(GameTime)”

'Sprite' 没有实现继承的抽象成员 'Component.Draw(GameTime, 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
}

组件类:

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

    public abstract void Update(GameTime gameTime);
}

我尝试修复此错误并实现将Draw方法更改为Override而不是 virtual 并添加Update override 方法。但这在第 33 行又犯了一个错误:

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

错误: System.NullReferenceException:“对象引用未设置为对象的实例。”

_texture 为空。

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

标签: c#monogame

解决方案


您必须virtual从这两种违规方法中删除,Draw并按照您所做的那样Update标记它们。override

你得到的唯一问题NullReferenceException是你的_texture领域是null. 为此,您必须确保_texture在用于构造实例的构造函数中实例化该字段Sprite

如果现在这一切对你来说太难理解,我建议你先复习一下 C# 的基础知识。您可以在MSDN和 youtube 上找到多种资源。


推荐阅读