问题
- (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