未将对象引用设置为对象的实例 #100
我知道人们以前问过这个问题,但他们的解决方案似乎对我不起作用,或者我做错了什么。
public class Sprite
{
private Game m_game;
private SpriteBatch m_spriteBatch;
private string m_filename;
private Texture2D m_texture;
public Sprite(Game game, SpriteBatch spriteBatch, GraphicsDevice graphicsDevice)
{
m_game = game;
m_spriteBatch = spriteBatch;
m_texture = new Texture2D(graphicsDevice, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height);
}
public void LoadSprite(string filename)
{
m_texture = m_game.Content.Load<Texture2D>(filename);
}
}
当我传递“tree”作为文件名时,在 LoadSprite 中会生成错误。 m_texture 不为 null,因为(尝试)在构造函数中初始化它。 对 Content.Load 的相同调用也可以在主循环中使用,但我想将其移至 Sprite 类中。
treeTexture = Content.Load<Texture2D>("tree");
这在主循环中工作得很好,因此它表明“树”文件存在。
谁能看到我做错了什么吗?
I know people have asked this before, but it would appear that their solution doesn't work for me or I'm doing something wrong.
public class Sprite
{
private Game m_game;
private SpriteBatch m_spriteBatch;
private string m_filename;
private Texture2D m_texture;
public Sprite(Game game, SpriteBatch spriteBatch, GraphicsDevice graphicsDevice)
{
m_game = game;
m_spriteBatch = spriteBatch;
m_texture = new Texture2D(graphicsDevice, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height);
}
public void LoadSprite(string filename)
{
m_texture = m_game.Content.Load<Texture2D>(filename);
}
}
The error is generated at LoadSprite when I pass "tree" as the filename.
m_texture isn't null because (tried to) initialise it in the constructor.
The same call to Content.Load is used in the main loop fine but I want to move that into the Sprite class.
treeTexture = Content.Load<Texture2D>("tree");
This works fine in the main loop so it shows that the "tree" file exists.
Can anyone see what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
m_game 或 m_game.Content 可能为 null。
m_game or m_game.Content is probably null.