Removing Sprites from Array on physicscollision

百般思念 提交于 2019-12-25 06:02:19

问题


    - (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;

}

I am getting an error:

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'.

What I want to happen is this: when a monster is hit, if it's lives are 0 it is removed from the physicsworld. No matter where abouts I put the removeobjects bit I get the same error. Thanks. This was fixed by moving the removeObjects bit outside the loop. This next bit is why I wanted it, to remove the monster from this array, maybe I am going about the wrong way and you can get it from the physics body monster I don't know.

    -(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);

    }


}

}

来源:https://stackoverflow.com/questions/24395950/removing-sprites-from-array-on-physicscollision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!