在physicscollision上从Array中删除Sprite

时间:2014-06-24 20:46:09

标签: objective-c cocos2d-iphone

    - (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair monsterCollision:    (CCNode *)monster projectileCollision:(CCNode *)projectile {

    for (CCSprite *spr in spriteArray)
    {
        if (_monster1.lives == 0) 
        {
            NSMutableArray *spritesToRemove = [[NSMutableArray alloc] init];                
            // Remove the sprite from its parent here.
            [spr removeFromParent];
            // However, leave the "spr" in ssprites array - just "mark" it for deletion.
            [spritesToRemove addObject:spr];

            if ([spritesToRemove count]> 0) {

                [spriteArray removeObjectsInArray:spritesToRemove];
            }
        }
    }

[projectile removeFromParent];
return YES;

}

我收到错误:

*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* - [__ NSArrayM objectAtIndex:]:索引0超出空数组的边界'。

我想要发生的是:当一个怪物被击中时,如果它的生命是0,它将从物理世界中移除。无论我把删除对象放在哪里,我都会得到同样的错误。谢谢。通过将removeObjects位移到循环外部来解决此问题。接下来就是为什么我想要它,从这个数组中删除怪物,也许我会走错路,你可以从我不知道的物理身体怪物那里得到它。

    -(void)update:(CCTime)delta
    {

 int multiplier = 0;

for (CCSprite* tempSprite in spriteArray)
{
    CGPoint nodePosition = tempSprite.position;


    if (nodePosition.x > 470) {

    multiplier++;

    structurelife = structurelife-(1*multiplier);

    }


}

}

0 个答案:

没有答案