title: Checking if a CCSprite exists, adding and removing from a CCScene, and looping until a condition is met
body: <p>I'm currently working on a game in which I add a CCSprite to a scene (an animated ghost), and have it move from a randomly generated point to another randomly generated point, fade out, and then be removed from the scene. When the player taps the ghost, 1 is added to the score.</p>
  
  <p>Everything is working beautifully, but I want this action to be repeated until a condition is met (specifically, when the score reaches 50). I can't figure out the best way to do this. Every time I try to run a loop, it repeats infinitely and crashes the game. </p>
  
  <p>What is the best way to achieve such a loop? I've heard mention of creating arrays for sprites, but there will only ever be one iteration of the ghost on the scene at one time. Are there any other ways of achieving this? </p>
  
  <p>Ideally, what I want to achieve is: run the actions, wait for them to finish, and then run them again until the score is 50. </p>
  
  <p>Here's my code:</p>
  
  <pre><code>-(void) ghostSpawner {
  
  
  CGPoint endPoint = [self randomPointGenerator];
  [self birthGhost];
  
  CCSprite * Ghost = (CCSprite *) [self getChildByTag:2];
  
  
  
  //...then fade him in
  CCFiniteTimeAction *ghostBegin = [CCSequence actions:[CCDelayTime actionWithDuration:1],[CCTintTo actionWithDuration:0 red:0 green:0 blue:0],[CCTintTo actionWithDuration:.5 red:255 green:255 blue:255], nil];
  
  //...move him over a given amount of time, then fade him out and DIE
  CCFiniteTimeAction *ghostEnd = [CCSequence actions:[CCMoveTo actionWithDuration:2     position:endPoint],[CCTintTo actionWithDuration:1 red:0 green:0 blue:0],[CCCallFunc actionWithTarget:self selector:@selector(killGhost)], nil] ;
  
  [Ghost runAction:[CCRepeatForever actionWithAction:[CCSequence actions:ghostBegin,ghostEnd, nil]]];
  }
  </code></pre>
  
  <p>For generating the sprite:</p>
  
  <pre><code>-(void) birthGhost {
  CGPoint spawnPoint = [self randomPointGenerator];  
  [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"ghostidle.plist"];
  
  CCSprite * Ghost = [CCSprite spriteWithSpriteFrameName:@"Ghost0001.png"];
  [Ghost setColor:ccc3(0,0,0)];
  Ghost.anchorPoint = ccp(.5, .5);
  Ghost.position = spawnPoint;
  Ghost.anchorPoint = ccp(0.5f,0.5f);
  Ghost.scale = .4;
  Ghost.tag = 2;
  
  [Ghost runAction:ghostIdleAnimation()];
  [self addChild:Ghost z:0];
  }
  </code></pre>
  
  <p>The 'ghostSpawner' function is called in my 'init' on the scene</p>
  
  <pre><code>[self ghostSpawner];
  </code></pre>
  
  <p>I hope this is enough. Any feedback would be awesome, since this is one of my first real iPhone projects. Thanks in advance!</p>