Search code examples
cocos2d-iphoneparameter-passingscene

How to pass parameters between cocos2d scenes


I am using

[[CCDirector sharedDirector] replaceScene:[CCTransitionCrossFade transitionWithDuration:0.5f scene:[CCBReader sceneWithNodeGraphFromFile:@"SongLoadingScene.ccbi"] ]];

For transition scene by scene. How to pass parameters to a scene.


Solution

  • CCBReader's sceneWithNodeGraphFromFile is just a class method that returns a new instance of CCBReader.

    So, if you want to pass an integer to it, first modify sceneWithNodeGraphFromFile to receive it, like

    +(CCScene*)sceneWithNodeGraphFromFile:(NSString*)file andInteger:(int)integer;
    

    And then modify CCBReader's constructor to also receive it. If currently it looks like

    -(id)initWithFile:(NSString*)file;
    

    You'd have

    -(id)initWithFile:(NSString*)file andInteger:(int)integer;
    

    Finally you modify sceneWithNodeGraphFromFile to pass the integer to this new constructor.