Search code examples
objective-cloopscocos2d-iphoneccsprite

Checking if a CCSprite exists, adding and removing from a CCScene, and looping until a condition is met


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.

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.

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?

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.

Here's my 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]]];
}

For generating the sprite:

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

The 'ghostSpawner' function is called in my 'init' on the scene

[self ghostSpawner];

I hope this is enough. Any feedback would be awesome, since this is one of my first real iPhone projects. Thanks in advance!


Solution

  • Alright, here's how I would do it:

    1. Send ghostSpawner to self on init (as you do now).

    2. In ghostSpawner, take your sprite, reset it (change the opacity to 0, reset the position, whatever), then send it 2 runAction messages: one to fade in, the other a sequence to move to the end position, then send a killGhost message to self.

    3. In killGhost, check whether the score is less than 50. If it is, send ghostSpawner to self again, otherwise do whatever you do when they have reached 50 points.

    Your sprite creation code should be in init. You can just reuse the same sprite. Also, there's CCFadeIn you can use instead of CCTintTo.