Hello Developers,
I’m James, one of the core developers in Cocos2D-X team. I work for Cocos about 6 years now.
In v3.16, I corrected a wrong logic code in Node::cleanup
. I was thinking about that Node::cleanup
removes Action
s, Scheduler
s, why wouldn’t it remove EventListener
s?
Look at this code in Node::pause
and Node::resume
:
void Node::resume()
{
_scheduler->resumeTarget(this);
_actionManager->resumeTarget(this);
_eventDispatcher->resumeEventListenersForTarget(this);
}
void Node::pause()
{
_scheduler->pauseTarget(this);
_actionManager->pauseTarget(this);
_eventDispatcher->pauseEventListenersForTarget(this);
So
void Node::cleanup()
{
...
...
// actions
this->stopAllActions();
// timers
this->unscheduleAllCallbacks();
// ???
// Event listeners should not be cleaned up / removed???
for( const auto &child: _children)
child->cleanup();
}
In v3.16, I thought that it should be fixed by invoking _eventDispatcher->removeEventListenersForTarget(this);
in Node::cleanup.
If a node is cleaned up, all its components attached to it should be removed.
And why don’t we remove the associated event listeners?
I didn’t consider the wrong use case of user code:
...
node->retain();
node->removeFromParent(); // or node->removeFromParentAndCleanup(true);
anotherNode->addChild(node);
node->release();
In fact, if developer doesn’t want to to cleanup node, developer should invoke node->removeFromParentAndCleanup(false)
.
I didn’t consider there are some developers write this wrong code. There even are some code in TableView which uses removeFromParent() in our engine.
Sorry of that I broke the compatibility of Node::cleanup behavior in v3.16.
If you’re using v3.16, writing code like above, and find your node can’t receive touch or other events, you may need to check your code and use removeFromParentAndCleanup(false);
instead of removeFromParent()
.
Or revert that line in Node::cleanup
.
I have sent a PR to revert the code :
The issue report is :
The reverted code may be in v3.17 which depends on what you’re thinking about this.
Any advice and thought will be appreciated.
Best Wishes
- James