开发者

Object reference not set to an instance of an object #100

开发者 https://www.devze.com 2023-04-12 18:28 出处:网络
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.

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 开发者_StackOverflow中文版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?


m_game or m_game.Content is probably null.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号