0
\$\begingroup\$

I'm writing a tetris clone, and I have sprites (of every possible piece and rotation) loaded through the XNA content pipeline. For example, I have assets "i", "i2", "i3", and "i4" for each rotation of the 4-block-long I-tetrimino. For the O-tetrimino, I only have asset "o", because the O-tetrimino doesn't require any rotations.

I want to load each of these assets into a 2D array of Texture2Ds. Here's my attempt:

1 Texture2D[,] tetriminoTextures = new Texture2D[7, 4];
2 char[] tetriminos = { 'o', 'i', 'l', 'j', 'z', 's', 't' };
3 string t;
4 
5 for (int i = 0; i < 7; i++)
6 {
7     for (int j = 1; i <= 4; i++)
8     {
9         if (i == 0 || j == 1) // If char[i] == 'o', then don't consider rotations.
10                              // The square piece doesn't have rotations.
11                              // If j == 1, then leave the asset name as is.
12            t = letters[i].ToString();
13        else // (i != 0 && j <= 2 && j <= 4)
14            t = letters[i].ToString() + j;
15        tetriminoTextures[i, j-1] = Content.Load<Texture2D>(t);           
16    }    
17 }

However, when I attempt to

spriteBatch.Draw(tetriminoTextures[x, y], /* some position vector */, Color.White);

I get an ArgumentNullException. Apparently tetriminoTextures[x, y] is null when y >= 1 && y <= 3.

I have no idea what's going on. I think it may have something to do with my concatenating a string and an int together in line 14. But I'm getting an ArgumentNullException even when i == 0 && j >= 2 && j <= 4 (i.e. for each rotation of the O-tetrimino excluding the first one), though the if statement in line 9 should have caught these cases. Any help would be appreciated!

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You should change the var i to j here

7 for (int j = 1; j <= 4; j++)
8     {
9    

Maybe you should use the step trace to debug your execution flow... worth it... :)

Anyway is a common copy/paste bug... hehehe

\$\endgroup\$
1
  • \$\begingroup\$ Damn, I got owned by a for loop for the past 3 hours. Thanks for pointing that out! Also, I'll upvote when I can. \$\endgroup\$ Commented May 3, 2012 at 22:36

You must log in to answer this question.

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