I keep getting the IndexOutOfRangeException for my XNA game and after printing the indexes it stops at 600. Is that the max limit for arrays in C#?

I'm trying to get all of the pixels from an 800x600 image into an array so I can test when a R:20 G:20 B:20 pixel is detected beneath the player, it stops the player from falling through.


I just realized one thing that might help is to make the image really small, only containing a few pixels. I tried it but it's all blurry, I'm still getting an index out of range, and I'm not sure how I would compare the pixel location to the player location unless I get the 800x600 pixels again

Edited 5 Years Ago by Slyvr: n/a

Attachments testsmall.png 0.54 KB Untitled.png 75.63 KB

No, 600 isn't the limit for the size of an array. Without seeing your code I can't tell you why you are exceeding your array size.

No, 600 isn't the limit for the size of an array. Without seeing your code I can't tell you why you are exceeding your array size.

This code sets the texture pixels to an array.

private Color[,] TextureTo2DArray(Texture2D texture)

    {
        Color[] colors1D = new Color[texture.Width * texture.Height];
        texture.GetData(colors1D);

        Color[,] colors2D = new Color[texture.Width, texture.Height];
        for (int x = 0; x < texture.Width; x++)
            for (int y = 0; y < texture.Height; y++)
                colors2D[x, y] = colors1D[x + y * texture.Width];
        return colors2D;
    }

This code processes the player falling and should stop falling when it hits the correct color pixel:

private void ProcessFalling(){

        Color levelpixel = LevelPixels[0, (int)player.getPlayerY()];
        if ((levelpixel.R != 20) && (levelpixel.G != 20) && (levelpixel.B != 20)){
            player.setPlayerY(player.getPlayerY() + player.getDescentSpeed());
        }
    }

In the first part, changing (int)player... to 0 doesn't throw the error, but there's no collisions that are going to be found. playerY should be 140

Edited 5 Years Ago by Slyvr: n/a

  • I'm not sure where your error is occuring. Is it line 3 of the second code block?
  • You say playerY should be 140, but what is it?
  • Have you checked the actual values of texture.Width and texture.Height in line 7 of your first code block?
  • Why do you get the colors (in line 10) from your 1D array (which you don't show the code for generating) instead of from the texture itself? By doing it the way you are you make the 2D array dependent upon the 1D array being correct. This is generally a bad practice as an error in one affects things outside of it.
  • When the exception is raised, what is the value of LevelPixels.Width, levelPixels.Height, player.getPlayerY?
  • I'm not sure where your error is occuring. Is it line 3 of the second code block?
  • You say playerY should be 140, but what is it?
  • Have you checked the actual values of texture.Width and texture.Height in line 7 of your first code block?
  • Why do you get the colors (in line 10) from your 1D array (which you don't show the code for generating) instead of from the texture itself? By doing it the way you are you make the 2D array dependent upon the 1D array being correct. This is generally a bad practice as an error in one affects things outside of it.
  • When the exception is raised, what is the value of LevelPixels.Width, levelPixels.Height, player.getPlayerY?

Color levelpixel = LevelPixels[0, (int)player.getPlayerY()];
This line causes the error.
playerY = 140
texture.width = 800
texture.height = 600
The TextureTo2DArray method was copied from Riemer's XNA tutorial, so I haven't really done anything to it. It does work because I've gotten the collision detection working before, but there were glitches with some other things that should be fixed now besides collision.

Well, something isn't working or you wouldn't be getting an array exception. LevelPixels must be 140 or less in the 2nd dimension. Focus on where it is defined and make sure you aren't changing the reference somewhere.

This article has been dead for over six months. Start a new discussion instead.