c# - 不断收到错误 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 为空。
谢谢,抱歉发了这么长的帖子,有什么办法解决这个问题吗?
解决方案
您必须virtual
从这两种违规方法中删除,Draw
并按照您所做的那样Update
标记它们。override
你得到的唯一问题NullReferenceException
是你的_texture
领域是null
. 为此,您必须确保_texture
在用于构造实例的构造函数中实例化该字段Sprite
。
如果现在这一切对你来说太难理解,我建议你先复习一下 C# 的基础知识。您可以在MSDN和 youtube 上找到多种资源。
推荐阅读
- amazon-web-services - 当 Chrome 选项卡未打开时,AWS Cloud9 实例的性能大幅下降
- java - 子 maven 项目找不到 maven-compiler-plugin
- web - 如何使用 WebXR 在我的房间中放置一些对象?
- arrays - 如何在 c 编程语言中动态分配 3d 矩阵?
- excel - 隐藏具有条件 VBA 的列
- php - 调用未定义的函数 getNameEC()
- c++ - 使用 CMake 链接 fortran 和 c++ - 跳过不兼容的...错误
- django - 在 django 中更改 FILE_UPLOAD_MAX_MEMORY_SIZE 的正确方法是什么?
- sql - 如何从 dd/mm/yy hh:mm:ss ms 获取月份和年份?
- flutter - 使用 Getx 小部件-Flutter 运行时显示错误