"Object reference not set to an instance of an object"

"Object reference not set to an instance of an object"

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    //Character
    Texture2D character;
    Texture2D background;
    Texture2D ground;
    //Movement
    Vector2 position = Vector2.Zero;
    Vector2 velocity;
    bool hasJumped;
    float movespeed = 500f;
    //IDK how to call this
    Rectangle mainFrame;
    //Collision
    Rectangle playerBounds, groundBounds;
    Color playerColor = Color.White;
    Rectangle player, enemy;
    Texture2D sprite1, sprite2;


    static bool IntersectsPixel(Texture2D sprite1,Texture2D sprite2, 
                                  Rectangle player, Rectangle enemy)
    {
        //Collision
        Color[] colordata1 = new Color[sprite1.Width * sprite1.Height]; 
        //ERROR ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ERROR UP THERE ^^^
        Color[] colordata2 = new Color[sprite2.Width * sprite2.Height];
        sprite1.GetData<Color>(colordata1);
        sprite2.GetData<Color>(colordata2);

        int top = Math.Max(player.Top, enemy.Top);
        int bottom = Math.Min(player.Bottom, enemy.Bottom);
        int left = Math.Max(player.Left, enemy.Left);
        int right = Math.Min(player.Right, enemy.Right);

        for (int y = top; y < bottom; y++)
            for (int x = left; x < right; x++)
            {
                Color A = colordata1[(y - player.Top) * (player.Width) + (x      
                                                            - player.Left)];
                Color B = colordata2[(y - enemy.Top) * (enemy.Width) + (x - 
                                                               enemy.Left)];

                if (A.A != 0 && B.A != 0)
                    return true;
            }
        return false;
    }

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

    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        //Sprite loading, setting possitions.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        character = Content.Load<Texture2D>("Character");
        background = Content.Load<Texture2D>("Background");
        ground = Content.Load<Texture2D>("Ground");
        mainFrame = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, 
                                  GraphicsDevice.Viewport.Height + 400);
        position = new Vector2(100,100);
        groundBounds = new Rectangle((int)mainFrame.X, (int)mainFrame.Y, 
                                           ground.Width, ground.Height);
    }

    protected override void UnloadContent()
    {
    }


    protected override void Update(GameTime gameTime)
    {
        //Movement
        if (Keyboard.GetState().IsKeyDown(Keys.Left) && position.X >= 0)
        {
            position.X -= movespeed * 
            (float)gameTime.ElapsedGameTime.TotalSeconds;

        }
        else if (Keyboard.GetState().IsKeyDown(Keys.Right) && position.X <=  
                           GraphicsDevice.Viewport.Width - character.Width)
        {
            position.X += movespeed *          
            (float)gameTime.ElapsedGameTime.TotalSeconds; 
        }

        //Jump
        position += velocity;
        if (Keyboard.GetState().IsKeyDown(Keys.Space) && hasJumped == false)
        {
            position.Y -= 10f;
            velocity.Y = -5f;
            hasJumped = true;
        }
        if (hasJumped == true)
        {
            float i = 1;
            velocity.Y += 0.15f * i;
        }

        if (position.Y + character.Height >= 500)
        {
            hasJumped = false;
        }

        if (hasJumped == false)
            velocity.Y = 0;
        //Collision
        playerBounds = new Rectangle((int)position.X, (int)position.Y,                      
                                   character.Width, character.Height);
        if (playerBounds.Intersects(groundBounds))
        {
            if (IntersectsPixel(sprite1, sprite2, playerBounds,            
                                                 groundBounds))
            {
                playerColor = Color.Aqua;
            }
            else
            {
                playerColor = Color.White;
            }
        }
        base.Update(gameTime);
    }


    protected override void Draw(GameTime gameTime)
    {
        //Sprite DRAW
        GraphicsDevice.Clear(Color.Aqua);
        spriteBatch.Begin();
        spriteBatch.Draw(character, position, playerColor);
        spriteBatch.Draw(ground, mainFrame, Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }

}

当我启动它时它显示 "Object reference not set to an instance of an object."

下面的代码

if (IntersectsPixel(sprite1, sprite2, playerBounds, groundBounds))

您正在尝试使用 sprite1sprite2,但您从未给它们赋值。

认为你应该使用下面的

if (IntersectsPixel(character, ground, playerBounds, groundBounds))

您已经声明了 sprite1sprite2,但您还没有在任何地方分配它们。

在这里,您调用 IntersectsPixel,传递 sprite1sprite2,但它们没有任何值。 (我猜你从某处复制粘贴了代码?)

if (playerBounds.Intersects(groundBounds))
    {
        if (IntersectsPixel(sprite1, sprite2, playerBounds,            
                                             groundBounds)) //Here
        {
            playerColor = Color.Aqua;
        }
        else
        {
            playerColor = Color.White;
        }
    }

从外观上看,您可能更想用 characterground 来调用它,如下所示:

if (playerBounds.Intersects(groundBounds))
    {
        if (IntersectsPixel(character, ground, playerBounds,            
                                             groundBounds))
        {
            playerColor = Color.Aqua;
        }
        else
        {
            playerColor = Color.White;
        }
    }