加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx中onExitTransitionDidStart、onExit和onEnter、onEnter

发布时间:2020-12-14 16:41:33 所属栏目:百科 来源:网络整理
导读:进入Node::addChildHelper定义发现如下代码 if( _running ) { child-onEnter(); // prevent onEnterTransitionDidFinish to be called twice when a node is added in onEnter if (_isTransitionFinished) { child-onEnterTransitionDidFinish(); } } 也就是

进入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);

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读