此 while 循环未将所需的值正确存储到矩形数组中。矩形用于从精灵表中拉出精灵。下面注释掉的代码完美运行。内循环非常适合设置第一行,但添加外循环会搞砸一切。sizeOfEachSpriteX 和 sizeOfEachSpriteY 的值都是 32(精灵是方形的)。spritesheet 大小为 96 宽 x 128 高。它是 4 行和 3 列的精灵。目标是将矩形声明为 Rectangles [哪一行,哪一列],正如注释掉的代码所实现的那样。似乎后面的迭代会覆盖第一次迭代的数据,并破坏整个过程。我很困惑!
int n = 0;
int n2 = 0;
while (n2 < 5)
{
while (n < 4)
{
Rectangles[n2, n] = new Rectangle
(sizeOfEachSpriteX * n2,
sizeOfEachSpriteY * n,
sizeOfEachSpriteX,
sizeOfEachSpriteY);
n++;
}
n2++;
}
/* This code works perfectly.
Rectangles[0,0] = new Rectangle(0,0,32,32);
Rectangles[0,1] = new Rectangle(0,32,32,32);
Rectangles[0,2] = new Rectangle(0,64,32,32);
Rectangles[0,3] = new Rectangle(0,96,32,32);
Rectangles[1,0] = new Rectangle(32,0,32,32);
Rectangles[1,1] = new Rectangle(32,32,32,32);
Rectangles[1,2] = new Rectangle(32,64,32,32);
Rectangles[1,3] = new Rectangle(32,96,32,32);
Rectangles[2,0] = new Rectangle(64,0,32,32);
Rectangles[2,1] = new Rectangle(64,32,32,32);
Rectangles[2,2] = new Rectangle(64,64,32,32);
Rectangles[2,3] = new Rectangle(64,96,32,32);
*/