Game Development Reference
In-Depth Information
#import "GameScene.h"
#import "GameState.h"
Listing 8-12
.
Presenting GameScene and loading the current level
+(void) presentGameScene
{
CCScene* scene = [CCBReader loadAsScene:@"GameScene"];
GameScene* gameScene
= (GameScene*)scene.children.firstObject;
int levelNumber = [GameState
sharedGameState].currentLevel;
NSString* level = [NSString stringWithFormat:@"Levels/
Level%i",
levelNumber];
[gameScene loadLevelNamed:level];
id t = [CCTransition
transitionPushWithDirection:CCTransitionDirectionLeft
duration:1.0];
[[CCDirector sharedDirector] presentScene:scene
withTransition:t];
}
The
GameScene
is loaded, as usual, using the
CCBReader
's
loadAsScene:
method.
I've mentioned this before, but it's worth noting again: the
loadAsScene:
method re-
turns a generic
CCScene
object whose only child is always the root node of the CCB that
was loaded. In this case, it's an instance of the
GameScene
class.
The current level number is then converted to a string of the format
Levels/Level%i
.
For example, the resulting string will be
Levels/Level1
for the first level or
Levels/Level2345
for the 2,345
th
level.
Tip
Developers still frequently pad file names with additional zeros to fight a
long-won battle: numeric sorting. If you feel tempted to format numbered items
by padding with zero—for instance,
Level0001
—because you might have up
to a thousand levels, please don't! Modern operating systems are well aware of
Search WWH ::
Custom Search