我最近一直试图制作游戏,并决定从头开始。但是,我很快遇到了一个错误,这个错误在我之前的游戏会话中没有问题。 我和我的朋友经历了一些可能导致错误的想法,但是我们无法找到。
显然,当我尝试使用不同的类(UI.UILoader)加载对象时,它会以某种方式创建错误。主要类如下:
namespace WindowsGame1.Classes
{
public static Game1 instance;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public MouseState mouseState;
public KeyboardState keyState;
public SpriteFont debugFont;
Instance inst;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
debugFont = Content.Load<SpriteFont>("debugFont");
inst.Load();
UI.UILoader.Load();
// TODO: use this.Content to load your game content here
}
可以在使用Texture2D尝试加载两个纹理的位置下方看到类UILoader。但是,当它尝试加载第一个纹理时,我收到'System.NullReferenceException'错误。
namespace WindowsGame1.Classes.UI
{
public static class UILoader
{
public static Texture2D cursorTexture;
public static Texture2D buttonTexture;
public static void Load()
{
cursorTexture = Game1.instance.Content.Load<Texture2D>("Interface/Cursor");
buttonTexture = Game1.instance.Content.Load<Texture2D>("Interface/Cursor");
}
}
}
我认为可能有些东西需要我使用不同的类来加载(我发现它很奇怪)所以我尝试将它全部移动到主类中的加载内容类,但是,它没有删除错误但是它不是在加载会话期间触发,而是在我尝试使用该对象时触发(在该对象应该被赋予一个值后因此不为空)
我不相信问题出现在我尝试使用的图片上,因为我已经将它添加到我正在使用的文件夹中(我会发布图片,但我没有足够的声誉)
如果有人知道这个错误可能是什么原因,我会很高兴在这里你的想法。