Synchronous CCLayers - running levels in complex defined order

54 views Asked by At

I'm building a game that has dozens of dynamically generated mini-levels. To simplify, let's say I've got three types of levels, A, B, and C, and each one can take an integer difficulty. LevelA, LevelB, and LevelC are subclasses of CCLayer, and I've been loading them into a GameScene that's a subclass of CCScene, one at a time. I'm not married to this method; it's just what's been working.

What I can't figure out is how to control the flow of levels in an overarching way.

The simple way to load a new level seems to be replicate the [[CCDirector sharedDirector] replaceScene:[newScene node]] model - so something like: [[GameScene sharedScene] replaceGameplayLayer:[LevelA newLevelWithDifficulty:5]]].

This works fine, but only if the call to the next level comes within the code for the current level. So if I'm in an A-level, I can load up a corresponding B-level every time I hit 10 points - something like:

-(void)checkScore {
    if (score >= 10) {
        [[GameScene sharedScene] replaceGameplayLayer:[LevelB newLevelWithDifficulty:currentLevelDifficulty]];
    }
}

And then do the same in the LevelB class to load up C-levels, or some other configuration.

But what if I want to plan out a level order? A1, B1, C1, C2, C3, C4, C5, A2, etc? I'd like to be able to treat each level as a synchronous line in a function. Like:

-(void)playGame {
    [LevelA newLevelWithDifficulty:1];
    [LevelB newLevelWithDifficulty:1];
    for (int i = 0; i < 5; i++) {
        [LevelC newLevelWithDifficulty:i];
    }
    // ...etc...
}

I'm not sure how to tackle the problem though. Is it even possible? It's easy enough to make that checkScore method just kill the current Level at 10 points, but how would I keep LevelB from happening until it did?

I hope this question makes sense. Any help you could offer would be appreciated.

1

There are 1 answers

3
YvesLeBorg On

you could create in your scene an array of levels in the appropriate sequence ... but creating them all ahead of time would be expensive (textures, etc ...). So devise a key system that uniquely identifies the level and difficulty. Next, your scene could register for a notification (read up on NSNotificationCenter and NSNotification), and each level could signal completion by posting a notification. You could have multiple notifications, for example one for levelComplete, gameOver and gameVictory.

When the scene gets the levelComplete notification, you could fade out the layer, look up the next level info, instanciate an instance of the class, add it, and fade it in. ....

just one way of doing something like that. Others available.

I have level descriptor plists that contain the class name, the music to play, victory and gamover conditions, etc .... and use a scheme like this to sequence levels without swapping scenes. Works for me. ymmv :)