未将对象引用设置为对象的实例 #100

发布于 2024-12-09 14:38:28 字数 888 浏览 0 评论 0原文

我知道人们以前问过这个问题,但他们的解决方案似乎对我不起作用,或者我做错了什么。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳絮泡泡 2024-12-16 14:38:28

m_game 或 m_game.Content 可能为 null。

m_game or m_game.Content is probably null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文