1

此 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);
*/
4

2 回答 2

1

您需要在每个内部 while 循环之前将 n 变量重置为 0,现在它达到 4,并且您的内部 while 循环将永远不会再次进入。

int n = 0;
int n2 = 0;
while (n2 < 5)
{
    n = 0;
    while (n < 4)
    {
        Rectangles[n2, n] = new Rectangle
                     (sizeOfEachSpriteX * n2, 
                      sizeOfEachSpriteY * n, 
                      sizeOfEachSpriteX, 
                      sizeOfEachSpriteY);
        n++;
    }
    n2++;
}
于 2012-07-16T15:47:57.417 回答
1

完成每个内部循环后,您需要将 n 重置为 0

int n = 0;
int n2 = 0;
while (n2 < 5)
    {
        n = 0;
        while (n < 4)
        {
            Rectangles[n2, n] = new Rectangle
                 (sizeOfEachSpriteX * n2, 
                  sizeOfEachSpriteY * n, 
                  sizeOfEachSpriteX, 
                  sizeOfEachSpriteY);
            n++;
        }
        n2++;
    }

发生的情况是,对于 n2 > 0,n 已经是 4,因此永远不会执行内部循环。

于 2012-07-16T15:48:59.037 回答