I'm trying to figure out the best way to pass a level parameter between Scenes using Spritebuilder and Cocos2D in Xcode.
I'm using the standard code below to transition between scenes.
[[CCDirector sharedDirector] replaceScene:[CCBReader loadAsScene:@"Gameplay"]];
Any help will be much appreciated.
Assuming that the Gameplay.ccb
has a GameplayClass
assigned as its custom class, and that class has a property named currentLevel
, you can access the instance and assign the level as follows:
CCScene* theScene = [CCBReader loadAsScene:@"Gameplay"];
GameplayClass* game = (GameplayClass*)theScene.children.firstObject;
game.currentLevel = 3;
[[CCDirector sharedDirector] replaceScene:theScene];
Note that by the time currentLevel
is assigned the GameplayClass
will have already run its init
and didLoadFromCCB
methods since that happens during loadAsScene
. If you need further init processing override onEnter
in GameplayClass
:
-(void) onEnter
{
[super onEnter]; // must call super
switch (self.currentLevel)
{
// other switches omitted...
case 3:
// your level 3 code here
break;
}
}