我對obj-c比較陌生,所以我必須錯過某些東西,但是當敵人與牆壁碰撞時,我的程序會崩潰。我找到了從循環中移除敵人的位置,而在循環中,但對於我的生活,我無法弄清楚如何修復它。 我的代碼如下:集合<__ NSArrayM:0x76c11b0>在枚舉時發生了變異
(錯誤是 「[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
好了,你能解釋正是做什麼。 [allEnemies copy]是否複製數組,並將其從原始「allEnemies」數組中移除。另外當你把「autorelease」放在那裏時,你不需要有「[allEnemies removeObject:enemyType1];」對?對不起,如果這聽起來真的很愚蠢(我剛剛得到了xcode 4,而且這還不是我3中的煩惱)。 編輯: 所以我改變了這段代碼,現在我得到: 無法註冊com.yourcompany.StromTheHouse與引導服務器。錯誤:未知的錯誤代碼。 – user1091516
好吧,xcode討厭我。所以我想我得到了「for(CCSprite * enemyType1 in [[allEnemies copy] autorelease])」,我沒有得到引導程序錯誤,但現在當我嘗試在設備上測試時,出現一個新錯誤: StormTheHouse [11129:707] cocos2d:無法添加圖片:牆。png in CCTextureCache 2011-12-10 11:02:05.888 StormTheHouse [11129:707] ***聲明失敗 - [HelloWorld addChild:],/Users/rauhul/Desktop/Invasion/libs/cocos2d/CCNode.m: 385 2011-12-10 11:02:05.890 StormTheHouse [11129:707] ***終止應用程序由於未捕獲的異常'NSInternalInconsistencyException',原因:'參數必須是非零' – user1091516
nvm這些帖子我想出了什麼是繼續,我在我的代碼中引用了wall.png,但圖像是WALL.png,Thx soooo – user1091516