从源码中也看到,如CCLayer::onEnter()中注册触摸事件,那么CCNode的onEnter函数是在什么时候调用的呢?
下面从源码角度分析下:
/**
* Adds a child to the container with z order and tag
*
* If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
*
* @param child A child node
* @param zOrder Z order for drawing priority. Please refer to setZOrder(int)
* @param tag A interger to identify the node easily. Please refer to setTag(int)
*/
//看上面的注释就可以清楚的知道,当我们当前的Node处于running时,那么添加到其上的
//子Node,会立即执行onEnter和onEnterTransitionDidFinish
virtual void addChild(CCNode* child, int zOrder, int tag);
-->>
void CCNode::addChild(CCNode *child, int zOrder, int tag)
{ .....
if( m_bRunning )
{
child->onEnter();
child->onEnterTransitionDidFinish();
}
}
假如我们有三级Node,第三级加到第二级上时,第二级还没有处于'running'状态,那就意味着我们的
我们的第三级Node的onEnter函数没有被执行,那么在什么时候执行呢?
void CCNode::onEnter()
{
//fix setTouchEnabled not take effect when called the function in onEnter in JSBinding.
m_bRunning = true;
if (m_eScriptType != kScriptTypeNone)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kCCNodeOnEnter);
}
//Judge the running state for prevent called onEnter method more than once,it's possible that this function called by addChild
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* child;
CCNode* node;
CCARRAY_FOREACH(m_pChildren, child)
{
node = (CCNode*)child;
if (!node->isRunning())
{
node->onEnter(); //循环遍历子Node,执行所有子Node的onEnter方法,我们的UI树一定会
//加入到一个处于'running'状态的Node,因为我们必然会至少有一个处于'running'
状态的Node,即使是sceen,所以上面的第三级Node的OnEnter函数
//会在第二级Node加入到第一级Node是执行(假设第一级Node处于'running'状态)。
}
}
}
this->resumeSchedulerAndActions();
}
CCNode 的onEnter函数何时调用分析
最新推荐文章于 2022-11-11 09:18:00 发布