Show Hide Layer

Show Hide Layer
0.0 0


I added a second layer(SettingLayer) to a scene and hide it like this :

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

    // 'layer' is an autorelease object
        MainPageLayer *layer = MainPageLayer::create();
    SettingLayer * settingLayer = SettingLayer::create();

    // add layer as a child to scene

    // return the scene
    return scene;

then tried to get that layer by tag and Show it like this:


But it didn’t work, I tried it with setVisible(true/false), didn’t work either.
Please advise what is the best way to show and hide layers in a scene.


You should not write scene() when you do such call. If your this class is your scene, try

That is how it works now: you call scene() method from this, it construct in memory some new scene (not current scene), in that new scene there is a Settings Layer that hided, after that you call showing of the layer, and it’s shows but on some new scene, that is not displayed now, and after that new scene will be released by Autorelease pool .


Hi Artur,
Thanks for your reply, indeed here (this) is a CCLayer (MainPageLayer)and I want to get the other layer (SettingLayer) with the mutual scene.
Is there any way to get scene from the layer without instantiating a new one?
I tried what you said:


It didn’t work because child with tag 2 does not exist.

Thanks again


Found it Artur,
I used getParent() and it did the trick:

CCScene * scene = (CCScene *)this->getParent();

Thanks for your help anyway.