集合 <__NSArrayM: 0x76c11b0> 在枚举时发生了变化

2023-11-25

我对 obj-c 比较陌生,所以我一定错过了一些东西,但是当敌人与墙壁碰撞时我的程序崩溃了。我已经找到了将敌人从循环中移除的位置,而在循环中,但对于我的一生,我不知道如何解决它。 我的代码如下:

(错误是“[allEnemies removeObject:enemyType1];”)

//始终运行 -(无效)更新:(ccTime)dt {

for (CCSprite *enemyType1 in allEnemies) { //for every attacking unit in allEnemies

    //Adjust the collison box for each enemey depending on the height of the enemy
    float a;
    float b;
    float yOne = (wall.contentSize.height-enemyType1.position.y);
    float yTwo = (wall.contentSize.height);
    float xTwo = 30;
    a = (xTwo*(yOne/yTwo)); // always < 1
    b = xTwo-a;             // always > 1


    //Create the altered collison box 
    CGRect enemyType1Rect = CGRectMake (
                enemyType1.position.x - (enemyType1.contentSize.width/2), 
                enemyType1.position.y - (enemyType1.contentSize.height/2), 
                enemyType1.contentSize.width+b, 
                enemyType1.contentSize.height
                                       );


    //If the enemey hits the wall, stop it, then add it to the attacking enemies array
    if (CGRectIntersectsRect(enemyType1Rect, wall.boundingBox)) {
        [enemyType1 stopAllActions];
        [allEnemies removeObject:enemyType1];
        [attackingEnemies addObject:enemyType1];            
    }


}
//Wall Collison END

好吧,正如错误所述,您在枚举数组时改变了数组。最简单的修复方法是for (CCSprite *enemyType1 in [[allEnemies copy] autorelease])这样,您就可以枚举数组的副本(这不会复制元素,只是为您提供另一个容器来枚举它们),并且仍然可以修改可变数组。

枚举容器时不能修改它们。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

集合 <__NSArrayM: 0x76c11b0> 在枚举时发生了变化 的相关文章

随机推荐