Game Development Reference
In-Depth Information
Since the number of levels is hardly ever predetermined in a game, you may be wondering
if you couldn't just count the levels? Well yes, you can!
You would have to count the number of Level#.ccbi files in the bundle's Published-iOS/
Levels directory. Adding Listing 8-20 to GameState.m (just above @end ) is entirely op-
tional. Take note, however, that if you do add it, unlocking more levels will work only if
you have consecutively named Level#.ccb files in SpriteBuilder.
Listing 8-20 . Counting the levels in the game
-(int) levelCount
{
NSBundle* mainBundle = [NSBundle mainBundle];
NSString* path;
int count = 0;
do
{
count++;
NSString* level = [NSString
stringWithFormat:@"Level%i", count];
path = [mainBundle pathForResource:level
ofType:@"ccbi"
inDirectory:@"Published-iOS/
Levels"];
} while (path != nil);
count--;
return count;
}
This do/while loop starts counting with one, generating a level string "Level1" . This
is passed into the pathForResource method of the NSBundle class. It doesn't look
for a file directly; rather, it requires you to pass in the individual path components: file
name, extension (here, called type ) and path ( directory ). The file name is "Level1" .
The extension is “ ccbi ” because published CCB files are converted to the binary ccbi
format. And the directory is “ "Published-iOS/Levels" . Therefore, it will look for
the file "Published-iOS/Levels/Level1.ccbi" in the root of the bundle.
Search WWH ::




Custom Search