Search code examples
androidc++cocos2d-x

How to get value from scene which i have pass from other scene in cocos2d for android?


I have PlayScene.cpp in this scene i have pass value like this: CCDirector::sharedDirector()->replaceScene( CCTransitionPageTurn::create(1.0f, ZeroScene::scene(3), true));

Now value is three (ZeroScene::scene(3)) and am able to get this value in ZeroScene

CCScene* ZeroScene::scene(int pups) { CCLog("selected pup is : %i",pups);

    // 'scene' is an autorelease object
CCScene *scene = CCScene::create();

// 'layer' is an autorelease object
ZeroScene *layer = ZeroScene::create();

// add layer as a child to scene
scene->addChild(layer);

// return the scene
return scene;

}

Now i want to use this int pups value in ZeroScene.cpp class. Please help me someone. Thanks in advance.


Solution

  • You have to write your own create function which will take this argument. Here is an example :

    In your *.h file add :

    static ZeroScene* create(int pups);
    

    And in your *.cpp :

    ZeroScene* ZeroScene::create(int pups) {
    
        ZeroScene *zs = new ZeroScene();
    
        if (zs->init()) {
            zs->autorelease();
            //here write your code to initialize everything
        } else
            zs = NULL;
    
        return zs;
    
    }
    

    Then you simply change the create line in scene() method. Everything that I wrote here is basic cocos2d-x initialization.