0
\$\begingroup\$

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

}

\$\endgroup\$
4
  • \$\begingroup\$ 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? \$\endgroup\$
    – test
    Dec 4, 2015 at 19:01
  • \$\begingroup\$ 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? \$\endgroup\$
    – hTeeML
    Dec 4, 2015 at 19:07
  • \$\begingroup\$ 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? \$\endgroup\$
    – test
    Dec 4, 2015 at 19:09
  • \$\begingroup\$ 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. \$\endgroup\$
    – hTeeML
    Dec 4, 2015 at 19:14

2 Answers 2

1
\$\begingroup\$

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()
    {

    }

    //your functions
    //...
}


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

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

yourClass a = new yourClass();
a.texture = Content.Load<Texture2D>("blabla");
a.position = new Vector2(0, 0, 800, 800);
list.Add(a);
//...
\$\endgroup\$
0
0
\$\begingroup\$

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);
     }
}
\$\endgroup\$
4
  • \$\begingroup\$ Oh I tried this too it didn't work either. \$\endgroup\$
    – hTeeML
    Dec 4, 2015 at 20:18
  • 1
    \$\begingroup\$ What about it didn't work? Same error? \$\endgroup\$
    – tandersen
    Dec 4, 2015 at 20:24
  • \$\begingroup\$ Yeah it was the same error again \$\endgroup\$
    – hTeeML
    Dec 4, 2015 at 21:01
  • \$\begingroup\$ Did you try copying and pasting my answer exactly? \$\endgroup\$
    – tandersen
    Dec 4, 2015 at 21:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .