I have a sprite (that appears infinitely on the screen) that should be removed by one of three ways:
1- Tap the sprite (adds to the score) 2- Tap a bomb that removes all sprites (adds to the score) 3- The sprite auto removes itself when it fades out (does not add to the score). The 3rd method is not working properly
For the first and second methods I’m using the general idea of :
for (ball in ballsArray)
{
if (CGRectContainsPoint(ball.boundingBox, location))
{
[ballsKillArray addObject:ball];
score = score +1;
sharedGameManager.score= score;
NSLog(@"%d",score);
}
}
for (ball in ballsKillArray)
{
[spriteSheet removeChild:ball cleanup:YES];
[ballsArray removeObject:ball];
[self removeChild:ball];
}
[ballsKillArray removeAllObjects];}
And for adding the sprite and performing the fadeout-then-remove I am using: (this is used every time a ball is being allocated which is done by an update method every certain time)
-(void)spawnBall:(ccTime)delta
{
ball = [[Ball alloc]init];
[spriteSheet addChild:ball];
[ballsArray addObject:ball];
id fade = [CCFadeOut actionWithDuration:3];
CCAction *removeBAll = [CCCallFuncN actionWithTarget:self selector:@selector (removeSelf)];
CCSequence *actionThenRemove = [CCSequence actions: fade,removeBall, nil];
[ball runAction:actionThenRemove];
}
And the removeSelf method to remove the sprite is:
-(void) removeSelf
{
for(ball in ballsArray)
{
[ballsKillArray addObject:ball];
}
for(ball in ballsKillArray)
{
[ballsArray removeObject:ball];
[self removeChild:ball cleanup:YES];
[spriteSheet removeChild:ball cleanup:YES];
}
[ballsKillArray removeAllObjects];}
The problem is i that the sprites after they perform the ccfadeout sequence are not removed as they should and i’m seeing weird behavior.
Any help is much appreciated.