CCNode 的onEnter函数何时调用分析

从源码中也看到,如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();   
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值