cocos2d: maintain state through levels

Quick hack if you don’t want to bother to add a singleton to your project and want to reload or swap a level while maintaining some data.

-(void) nextLevel {
	CCScene *scene = [[game_ class] scene];

	for (CCNode *node in scene.children) {
		if ([node isKindOfClass:[game_ class]]) {
			GameNode *gn = (GameNode*)node;
			gn.level = game_.level;
			gn.totalScore = game_.totalScore;

			break;
		}
	}
	[[CCDirector sharedDirector] replaceScene: [CCTransitionMoveInT transitionWithDuration:1.0f scene:scene]];
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: