不知道为什么循环不起作用?

时间:2011-03-01 14:08:24

标签: objective-c loops for-loop cocos2d-iphone

在调试器中它崩溃并且我收到此错误但索引号处的对象每次都在不断变化。   *由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* - [NSMutableArray insertObject:atIndex:]:索引15超出空数组的边界'

    #define NUMPOINTS 16
    #define NUMSPRITES 16   

    int myXPoints [NUMPOINTS];
    int myYPoints [NUMPOINTS];
    CCSprite *mySpritePointers [NUMSPRITES];

    NSMutableArray *mySpriteArray = [[NSMutableArray alloc] initWithCapacity:16];
    [mySpriteArray insertObject:sprite1 atIndex:0];
    [mySpriteArray insertObject:sprite2 atIndex:1];
    [mySpriteArray insertObject:sprite3 atIndex:2];
    [mySpriteArray insertObject:sprite4 atIndex:3];
    [mySpriteArray insertObject:sprite5 atIndex:4];
    [mySpriteArray insertObject:sprite6 atIndex:5];
    [mySpriteArray insertObject:sprite7 atIndex:6];
    [mySpriteArray insertObject:sprite8 atIndex:7];
    [mySpriteArray insertObject:sprite9 atIndex:8];
    [mySpriteArray insertObject:sprite10 atIndex:9];
    [mySpriteArray insertObject:sprite11 atIndex:10];
    [mySpriteArray insertObject:sprite12 atIndex:11];
    [mySpriteArray insertObject:sprite13 atIndex:12];
    [mySpriteArray insertObject:sprite14 atIndex:13];
    [mySpriteArray insertObject:sprite15 atIndex:14];
    [mySpriteArray insertObject:sprite16 atIndex:15];

    mySpritePointers [0] = (CCSprite *)[mySpriteArray objectAtIndex:0];
    mySpritePointers [1] = (CCSprite *)[mySpriteArray objectAtIndex:1];
    mySpritePointers [2] = (CCSprite *)[mySpriteArray objectAtIndex:2];
    mySpritePointers [3] = (CCSprite *)[mySpriteArray objectAtIndex:3];
    mySpritePointers [4] = (CCSprite *)[mySpriteArray objectAtIndex:4];
    mySpritePointers [5] = (CCSprite *)[mySpriteArray objectAtIndex:5];
    mySpritePointers [6] = (CCSprite *)[mySpriteArray objectAtIndex:6];
    mySpritePointers [7] = (CCSprite *)[mySpriteArray objectAtIndex:7];
    mySpritePointers [8] = (CCSprite *)[mySpriteArray objectAtIndex:8];
    mySpritePointers [9] = (CCSprite *)[mySpriteArray objectAtIndex:9];
    mySpritePointers [10] = (CCSprite *)[mySpriteArray objectAtIndex:10];
    mySpritePointers [11] = (CCSprite *)[mySpriteArray objectAtIndex:11];
    mySpritePointers [12] = (CCSprite *)[mySpriteArray objectAtIndex:12];
    mySpritePointers [13] = (CCSprite *)[mySpriteArray objectAtIndex:13];
    mySpritePointers [14] = (CCSprite *)[mySpriteArray objectAtIndex:14];
    mySpritePointers [15] = (CCSprite *)[mySpriteArray objectAtIndex:15];

    myXPoints [0] = 61;
    myXPoints [1] = 61;
    myXPoints [2] = 61;
    myXPoints [3] = 61;
    myXPoints [4] = 180;
    myXPoints [5] = 180;
    myXPoints [6] = 180;
    myXPoints [7] = 180;
    myXPoints [8] = 300;
    myXPoints [9] = 300;
    myXPoints [10] = 300;
    myXPoints [11] = 300;
    myXPoints [12] = 420;
    myXPoints [13] = 420;
    myXPoints [14] = 420;
    myXPoints [15] = 420;

    myYPoints [0] = 37;
    myYPoints [1] = 110;
    myYPoints [2] = 182;
    myYPoints [3] = 253;
    myYPoints [4] = 37;
    myYPoints [5] = 110;
    myYPoints [6] = 182;
    myYPoints [7] = 253;
    myYPoints [8] = 37;
    myYPoints [9] = 110;
    myYPoints [10] = 182;
    myYPoints [11] = 253;
    myYPoints [12] = 37;
    myYPoints [13] = 110;
    myYPoints [14] = 182;
    myYPoints [15] = 253;

    for (int nSprite = 0;  nSprite < NUMSPRITES;  nSprite++){
        for (int nPoint = 0;  nPoint < NUMPOINTS;  nPoint++){
            c1array = [[NSMutableArray alloc] initWithCapacity:16];
            if(mySpritePointers [nSprite].position.x == myXPoints [nPoint] && mySpritePointers [nSprite].position.y == myYPoints [nPoint]){
                [c1array insertObject:mySpritePointers [nSprite] atIndex:nPoint];
            } 
        }
    }

4 个答案:

答案 0 :(得分:2)

答案是-initWithCapacity:没有按照你的想法做到。您没有获得一个包含16个空插槽的数组,您可以在任何位置插入对象。您仍然得到一个空的([array count] == 0)数组,并尝试在除位置0之外的任何位置插入对象将失败。容量参数只是为运行时提供了有关阵列可能增长的高级提示。

使您的阵列实际上有16个插槽,以便您可以在任何索引处插入/替换&lt; 16,你需要添加一些对象,例如NSNull。

 NSMutableArray* myArray = [[NSMutableArray alloc] initWithCapacity: 16];
 for (int i = 0 ; i < 16 ; ++i)
 {
     [myArray addObject: [NSNull null]];
 }

 // Now the array has a count of 16.

我不太确定你要做什么,但我怀疑,在你的for循环中,你应该使用-replaceObjectAtIndex:withObject:而不是-insertObjectAtIndex:

答案 1 :(得分:1)

每次循环都会重新分配c1array。你可能不想这样做。

答案 2 :(得分:0)

你不应该在两个for循环(nSprite和nPoint)之间分配c1array吗?

答案 3 :(得分:0)

for (int nSprite = 0;  nSprite < NUMSPRITES;  nSprite++){
    for (int nPoint = 0;  nPoint < NUMPOINTS;  nPoint++){
        c1array = [[NSMutableArray alloc] initWithCapacity:16];
        if(mySpritePointers [nSprite].position.x == myXPoints [nPoint] && mySpritePointers [nSprite].position.y == myYPoints [nPoint]){
            [c1array insertObject:mySpritePointers [nSprite] atIndex:nPoint];
        } 
    }
}

如上所述,每次循环运行时都会重新分配NSMutableArray。试试这个:

c1array = [[NSMutableArray alloc] initWithCapacity:16];
for (int nSprite = 0;  nSprite < NUMSPRITES;  nSprite++){
    for (int nPoint = 0;  nPoint < NUMPOINTS;  nPoint++){
        if(mySpritePointers [nSprite].position.x == myXPoints [nPoint] && mySpritePointers [nSprite].position.y == myYPoints [nPoint]){
            [c1array insertObject:mySpritePointers [nSprite] atIndex:nPoint];
        } 
    }
}

Aslo我看不出有什么理由来创建你正在创建的东西:S

您可以使用循环代替重新输入大部分代码;)