Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XNA NullReferenceException我认为LoadContent赢了';跑不动_C#_Xna_Runtime Error - Fatal编程技术网

C# XNA NullReferenceException我认为LoadContent赢了';跑不动

C# XNA NullReferenceException我认为LoadContent赢了';跑不动,c#,xna,runtime-error,C#,Xna,Runtime Error,我正在创建一个xna游戏,它编译得很好,但是当它到达spriteBatch.Begin()时会抛出一个nullreferenceexception;在我的spritemanager类中(这是一个可绘制的游戏组件)。我认为loadContent没有运行,没有初始化spritebatch,但我不确定如何让它运行 SpriteManager加载内容代码: protected override void LoadContent() { spriteBatch = new

我正在创建一个xna游戏,它编译得很好,但是当它到达spriteBatch.Begin()时会抛出一个nullreferenceexception;在我的spritemanager类中(这是一个可绘制的游戏组件)。我认为loadContent没有运行,没有初始化spritebatch,但我不确定如何让它运行

SpriteManager加载内容代码:

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(Game.GraphicsDevice);  

        player = new Player(Game.Content.Load<Texture2D>("Images/asteroids_player"),
                            new Vector2(Game.Window.ClientBounds.X / 2, Game.Window.ClientBounds.Y / 2),
                            Vector2.Zero,
                            0f,
                            new Point( 27, 40 ),
                            new Point( 1, 1 ),
                            new Point( 1, 2 ));

        asteroidList = new List<Asteroid>();

        base.LoadContent();
    }
游戏1代码:

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    SpriteManager spriteManager;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // Creates and loads a SpriteManager to update/draw sprite objects
        spriteManager = new SpriteManager(this);
        Components.Add(spriteManager);

        base.LoadContent();
    }

    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }

在调用
base.Initialize()
之前,在
Initialize()方法中添加SpriteManager组件。Initialize()

您可以发布
类播放器(Texture2d Texture2d…)
?几乎所有
NullReferenceException
的情况都是相同的。请参阅“”以获取一些提示。如果您认为LoadContent无法运行,您应该尝试自己调试。在那里放一个断点,看看它是否没有运行。谢谢你,我在loadContent方法中意外地使用了断点,这是一个愚蠢的错误,但我可能要花几个小时才能找到。
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    SpriteManager spriteManager;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // Creates and loads a SpriteManager to update/draw sprite objects
        spriteManager = new SpriteManager(this);
        Components.Add(spriteManager);

        base.LoadContent();
    }

    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }