Search code examples
cocos2d-x

The layer does not destruct when the Scene is replaced


Anybody has the same problem? I've add the layer to the scene. But when the scene exited and called its destruct method, the layer's destruct method was not called. Here is the code:

void TowerScene :: init()
{

    addchild(Tower::create());
    retrun true;
}

Any help is appreciated.


Solution

  • CCTouchDispatcher, CCKeyboardDispatcher, CCScheduler or other singletons use retain on delegate object when it's added, so, reference counter remains incremented until delegate removed. If you manually add the layer as delegate to scheduler or other, you must manually remove (unshedule) delegate inside onExit method.

    To find problem you also can override virtual void retain() public method and set breakpoint in it. Backtrace will show, where reference counter was incremented.

    P.S. Be sure, that super-methods of init, onEnter, onExit and other overrided methods are called. C++ way: SuperClass::method()