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"));
blockTexture.Add(Content.Load<Texture2D>("right"));
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);
}
blockTexture
orblockPositions
array with an invalid index. My guess is that it is theblockTexture
array since you are correctly looping on theblockPositions
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 theblockTexture
array? \$\endgroup\$blockTexture
has 2 elements in it andblockPositions
has 3 elements in it? \$\endgroup\$