# How to draw the elements that are in a Texture2D list in XNA

I have a Texture2D list called blockTexture that has 2 elements in it.

I use the following line to draw the first element.

foreach (Vector2 blockPosition in blockPositions)
for (int i = 0; i < blockPositions.Count; i++)
{
spriteBatch.Draw(blockTexture[i], blockPositions[i], Color.White);
}


This allows me to draw the elements in different positions. However, while I run it after like 3-4 seconds it gives OutOfRangeArgument error.

I was wondering what am I doing wrong or is there another way to do this. I'm really sorry if my question sounds stupid but I'm a total beginner.

PS: this is a game where different type of objects fall from the top of the screen to the bottom, and the person at the bottom needs to avoid some of the objects and try to catch the others.

List<Vector2> blockPositions = new List<Vector2>();
List<Texture2D> blockTexture = new List<Texture2D>();


These are in the load content

blockTexture.Add(Content.Load<Texture2D>("Block"));


These are in the update section

for (int i = 0; i < blockPositions.Count; i++)
{
// Animate this block falling
blockPositions[i] =
new Vector2(blockPositions[i].X, blockPositions[i].Y + BlockFallSpeed);

// Get the bounding rectangle of this block
Rectangle sprite =
new Rectangle((int)blockPositions[i].X, (int)blockPositions[i].Y,
blockTexture[0].Width, blockTexture[0].Height);

// check collision with person
if (personRectangle.Intersects(sprite))
personHit = true;

// Remove this block if it have fallen off the screen
if ((blockPositions[i].Y > Window.ClientBounds.Height))
{
blockPositions.RemoveAt(i);
// When removing a block, the next block will have the same index
// as the current block. Decrement i to prevent skipping a block.
i--;
}
}

base.Update(gameTime);


}

• There is information missing from this post. Firstly, that exception is very self-explanatory. Literally, you're indexing into the blockTexture or blockPositions array with an invalid index. My guess is that it is the blockTexture array since you are correctly looping on the blockPositions one. How are each populated? Are you 100% sure that they have the same item count? Do you have an update procedure that is modifying the blockTexture array? – test Dec 4 '15 at 19:01
• blockTexture has 2 items where as blockPositions is points that is made randomly for the whole game screen. Thank you for your reply.. Do you know any way I can solve this problem please? – hTeeML Dec 4 '15 at 19:07
• You mentioned you are a beginner, so let's just take a step back here. What do you think would happen in your code if blockTexture has 2 elements in it and blockPositions has 3 elements in it? – test Dec 4 '15 at 19:09
• I understand that it will not work but I just don't know any other ways how to deal with this problem that's why I wanted to get help. And I appreciate so much that you replied. – hTeeML Dec 4 '15 at 19:14

I would do it so, so it can be safer and maybe easier to use :

Just use a class to hold the information of position and texture.

class yourClass
{
Texture2D texture;
Vector2 position;

public yourClass()
{

}

//...
}


And then you can use a List of this class, like:

List<yourClass> list = new List<yourClass>();

yourClass a = new yourClass();
a.position = new Vector2(0, 0, 800, 800);
//...


This is a bit of a shot in the dark, since I'm just guessing at your intentions here. But I think you would want to change your drawing code to the following:

foreach (Vector2 blockPosition in blockPositions)
{
for (int i = 0; i < blockTexture.Count; i++)
{
spriteBatch.Draw(blockTexture[i], blockPosition, Color.White);
}
}

• Oh I tried this too it didn't work either. – hTeeML Dec 4 '15 at 20:18
• What about it didn't work? Same error? – tandersen Dec 4 '15 at 20:24
• Yeah it was the same error again – hTeeML Dec 4 '15 at 21:01
• Did you try copying and pasting my answer exactly? – tandersen Dec 4 '15 at 21:06