views:

364

answers:

2

I'm making a game with cocos2d and I have a bunch of sprites that I would like to delete. For example I might have a bunch of characters on the screen but when my game is over I would like to clean them up. Right now I have created a special effect (particle system) as a distraction, but because it is transparent and does not cover all of the screen you can see through and watch the sprites disappear as I remove them from the layer.

Also because the instructions execute so fast to the user it appears as if the sprites disappear before the particle effect begins!

Any suggestions on my 2 problems? Thanks.

NSMutableArray *toRemove = [[NSMutableArray alloc] init]; // array of sprites that I collect to remove

spriteCount = 0;
 if([self findAllSprites:parent forRemoval:toRemove] > 0){ // is there is at least one sprite to delete. If not then don't do anything
  [self specialEffect]; // runs for maybe 3 seconds. 
                    // how can I stall here so that the sprites aren't removed "instantaneously"?
  for (Character* aCharacter in toRemove) {
   [aCharacter.parent remove:aCharacter];  
  }

}

+1  A: 

You need to do your 'special effect' in a thread, so that it runs alongside your sprite remove. Lookup NSThread for more information, but this will enable you to synchronize the two processes.

Chaos
+1  A: 

You can delay the removal action using performSelector:withObject:afterDelay:. For example:

    NSMutableArray *toRemove = [[NSMutableArray alloc] init]; // array of sprites that I collect to remove
    spriteCount = 0;
    if([self findAllSprites:parent forRemoval:toRemove] > 0){ // is there is at least one sprite to delete. If not then don't do anything
        [self specialEffect]; // runs for maybe 3 seconds. 
        [self performSelector:@selector(removeSprites:) withObject: toRemove afterDelay:1.0];
    }
    [toRemove release];

- (void) removeSprites: (NSArray*) toRemove
{
    for (Character* aCharacter in toRemove) {
        [aCharacter.parent remove:aCharacter];          
    }
}

Note that performSelector:withObject:afterDelay: will retain the toRemove object and keep it alive until after it calls removeSprites, so you don't have to do anything special with toRemove (except that you still need to release it as shown since you own it as well).

Peter N Lewis
Thanks! That worked wonderfully.
Stu