2

I want to create a starry sky for some scenes. The main problem is it needs some time to fill all the screen with particles. Someone advices me to create the whole sky at the beginning and save it between its calls. I tried something like this:

@implementation StarrySky

static StarrySky *_starrySky;

- (id)init
{
    if ((self = [super init])) {
        NSArray *starsArray = [NSArray arrayWithObjects:@"Stars1.plist", @"Stars2.plist", @"Stars3.plist", nil];
        for(NSString *stars in starsArray) {        
            CCParticleSystemQuad *starsEffect = [CCParticleSystemQuad particleWithFile:stars];        
            [self addChild:starsEffect z:-2];
        }
    }
    return self;
}

+ (StarrySky *)sharedStarrySky
{
    if (!_starrySky) {
        _starrySky = [[StarrySky alloc] init];
    }
    return _starrySky;
}

- (void)dealloc
{
    _starrySky = nil;
    [super dealloc];
}

@end

But the particles stop moving.

Gargo
  • 704
  • 6
  • 16
  • Your idea is right, however, Cocos stops the layer's timers when it's removed from it's parent. What you need to do is to look into the scheduler and find a way to either restore the timers or make sure they are not stopped. You can also ask around on the cocos forum. – EmilioPelaez Jun 11 '12 at 08:26

1 Answers1

2

One solution that ought to work is to create the star particle system and add it to your first scene (ie main menu). Set it to invisible or move it way off the screen or behind the background image (lowest z-order) so it can create some stars without actually showing any stars in the menu.

Now when you switch to your actual game scene, you alloc/init the game scene and then remove the star particle system from the currently running scene and add it to the game scene. Basically you're moving it from one scene to another.

Now you can call replaceScene with the game scene which now contains the already active star particle system.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Your advice is almost useful. Changing z-order works only. Setting visibility doesn't work properly. The result of visible=NO is particles are generated when they become visible only. The second thing you missed is surrounding your code with retain/release of starry sky object (of course, if you didn't save it twice) – Gargo Jun 12 '12 at 07:53