CCNode::onEnter()详解

本文为firedragonpzy原创,转载务必在明显处注明:
转载自【Softeware MyZone】原文链接: http://www.firedragonpzy.com.cn/index.php/archives/548


   今天遇到一个问题,自己封装了一个精灵(继承了精灵类和目标代理),里边实现了一个动画效果,但是调用的时候却没有动画,只是静态的图片。和同事们反复研究后发现原来是在重写的onEnter中忘记了调用CCSprite::onEnter(),这是很多童鞋们容易忽略的问题,在重写cocos2d-x组件的时候,总是忘了调用一下要重写组件的方法,亲们一定要注意了哦。现在由详细分析一个onEnter()这个方法。
    CCSprite直接调用了CCNode的onEnter(),由于CCLayer需要进行触屏事件的处理,所以重写了一下,处理了触屏事件,但是最终还是调用了CCNode::onEnter();最终问题都归结于CCNode::onEnter()

    大家先看下CCNode源码:void CCNode::onEnter()
{
	arrayMakeObjectsPerformSelector(m_pChildren, &CCNode::onEnter);

	this->resumeSchedulerAndActions();

	m_bIsRunning = true;
}

void CCNode::onEnterTransitionDidFinish()
{
	arrayMakeObjectsPerformSelector(m_pChildren, &CCNode::onEnterTransitionDidFinish);
}

void CCNode::onExit()
{
	this->pauseSchedulerAndActions();

	m_bIsRunning = false;

	arrayMakeObjectsPerformSelector(m_pChildren, &CCNode::onExit);
}

arrayMakeObjectsPerformSelector方法如下:
void CCNode::arrayMakeObjectsPerformSelector(CCArray* pArray, callbackFunc func)
{
	if(pArray && pArray->count() > 0)
	{
        CCObject* child;
        CCARRAY_FOREACH(pArray, child)
        {
            CCNode* pNode = (CCNode*) child;
            if(pNode && (0 != func))
            {
                (pNode->*func)();
            }
        }
	}
}

在onEnter中调用方法arrayMakeObjectsPerformSelector,形参m_pChildren为子节点,&CCNode::onEnter为本函数的引用,在方法中遍历,最经典的就是(pNode->*func)(),性质似函数指针,详情可参考[url]http://firedragonpzy.iteye.com/blog/1615710[/url],未完,待续……

你可能感兴趣的:(cocos2d-x)