进入Node::addChildHelper定义发现如下代码
if( _running )
{
child->onEnter();
// prevent onEnterTransitionDidFinish to be called twice when a node is added in onEnter
if (_isTransitionFinished) {
child->onEnterTransitionDidFinish();
}
}
也就是说,只有父节点已经在running的情况下,添加子节点时才会调用子节点的onEnter,由此可见,在scene中的添加的子节点,如果scene没有被渲染,那么之前在scene中添加的子节点(包括layer,sprite)时不会立刻调用子节点的onEnter。那么scene的onEnter()在什么时候调用呢?查看Director中的DrawScene()定义,发现如下代码:
/* to avoid flickr, nextScene MUST be here: after tick and before draw.
XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */
if (_nextScene)
{
setNextScene();
}
查看setNextScene定义发现
void Director::setNextScene()
{
bool runningIsTransition = dynamic_cast<TransitionScene*>(_runningScene) != nullptr;
bool newIsTransition = dynamic_cast<TransitionScene*>(_nextScene) != nullptr;
// If it is not a transition, call onExit/cleanup
if (! newIsTransition)
{
if (_runningScene)
{
_runningScene->onExitTransitionDidStart();
_runningScene->onExit();
}
// issue #709. the root node (scene) should receive the cleanup message too
// otherwise it might be leaked.
if (_sendCleanupToScene && _runningScene)
{
_runningScene->cleanup();
}
}
if (_runningScene)
{
_runningScene->release();
}
_runningScene = _nextScene;
_nextScene->retain();
_nextScene = nullptr;
if ((! runningIsTransition) && _runningScene)
{
_runningScene->onEnter();
_runningScene->onEnterTransitionDidFinish();
}
}
在场景类型不是TransitionScene类型时,当当前场景不为null时,先退出当前场景,先调用当前场景的onExitTransitionDidStart、onExit并释放当前场景,接着讲下一个场景赋值给当前场景_runningScene = _nextScene;接着调用当前场景的onEnter()、onEnterTransitionDidFinish(),进行自身以及已添加子节点的onEnter调用,每个节点会在自身的onEnter中设置_running = true;这样后续在已有场景中添加子节点时,会直接调用子节点的onEnter。
在已渲染场景中,子节点动态从父类中移除时,会调用onExitTransitionDidStart以及onExit
void Node::detachChild(Node *child, ssize_t childIndex, bool doCleanup)
{
// IMPORTANT:
// -1st do onExit
// -2nd cleanup
if (_running)
{
child->onExitTransitionDidStart();
child->onExit();
}
#if CC_USE_PHYSICS
child->removeFromPhysicsWorld();
#endif
// If you don't do cleanup, the child's actions will not get removed and the
// its scheduledSelectors_ dict will not get released!
if (doCleanup)
{
child->cleanup();
}
// set parent nil at the end
child->setParent(nullptr);
_children.erase(childIndex);
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。