-1
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);
    }

}

When I launch it it shows "Object reference not set to an instance of an object."

Nathan Tuggy
  • 2,239
  • 27
  • 28
  • 36
Lasinys
  • 1
  • 4
  • 1
    You're starting to learn programming by creating games ? Good luck with that ! – thomasb Feb 20 '15 at 15:29
  • Heh, unfortunately cosmo0 is kinda right here. It doesn't help that your error, is the most common error in programming. See http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – jgallant Feb 20 '15 at 15:38
  • If either of the answers helped you, could you accept one please. – wjh Feb 21 '15 at 10:45
  • You see i already know how, to program C++, but i have no ideas where to use it. So i'm similiar to programming and want to learn how C works. – Lasinys Feb 21 '15 at 13:57

2 Answers2

1

The below code

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

You are trying to use sprite1 and sprite2 but you never give them a value.

I think you should be using the below

if (IntersectsPixel(character, ground, playerBounds, groundBounds))
Oyyou
  • 436
  • 3
  • 12
0

You've declared sprite1 and sprite2, yet you have not assigned them anywhere.

Here, you call IntersectsPixel, passing sprite1 and sprite2, but they don't have any value. (I'm guessing you copy-pasted the code from somewhere?)

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

By the looks of things, you may well want to call it with character and ground instead, like so:

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