我一直在研究一款遊戲,並且我沒有除去敵方遊戲對象的問題(現在它們從CCSprite
分類,我知道這不是最好的方法)Cocos2d NSMutable Array在枚舉時發生了變異
但我不知道我改變了什麼,使它在程序試圖從_targets
加入targetsToDelete
後嘗試removeChild
時發生崩潰。
我嘗試過移動東西,我只是不知道如何在創建數組時添加或編輯數組......任何幫助或建議都會很棒!
而實際上,如果您有任何關於如何最好地創建遊戲敵人的指針,您是否劃分NSObject
或CCNode
?我聽說將它們分成組件類,但我不知道它們的含義。
//Projectile Target collision
-(void)update:(ccTime)dt {
for (spygot *target in _targets) {
CGRect targetRect = CGRectMake(
target.position.x - (target.contentSize.width/2),
target.position.y - (target.contentSize.height/2),
target.contentSize.width,
target.contentSize.height);
//Collision Detection Player
CGRect playerRect2 = CGRectMake(
_controlledSprite.position.x - (_controlledSprite.contentSize.width/2),
_controlledSprite.position.y - (_controlledSprite.contentSize.height/2),
_controlledSprite.contentSize.width,
_controlledSprite.contentSize.height);
NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
for (Projectile *projectile in _projectiles)
{
NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
CGRect projectileRect = CGRectMake(
projectile.position.x - (projectile.contentSize.width/2),
projectile.position.y - (projectile.contentSize.height/2),
projectile.contentSize.width,
projectile.contentSize.height);
BOOL monsterHit = FALSE;
if (CGRectIntersectsRect(projectileRect, targetRect))
{
NSLog(@"hit");
target.mhp = target.mhp - 1;
monsterHit = TRUE;
if (target.mhp <= 0)
{
[targetsToDelete addObject:target];
}
}
for (spygot *target in targetsToDelete)
{
[self removeChild:target cleanup:YES];
[_targets removeObject:target];
}
if (monsterHit)
{
[projectilesToDelete addObject:projectile];
}
[targetsToDelete release];
}
for (Projectile *projectile in projectilesToDelete)
{
[_projectiles removeObject:projectile];
[self removeChild:projectile cleanup:YES];
}
[projectilesToDelete release];
}
你寫道:「當[嘗試]從_targets中removeChild時崩潰」。爲了清楚起見,你的意思是說有問題的行是'[self removeChild:target cleanup:YES];''或'[_targets removeObject:target];'? –