您的位置:首页 > Web前端 > Node.js

CCNode 代码分析

2014-08-29 22:29 337 查看
void CCNode::sortAllChildren()
{
if
(m_bReorderChildDirty)
{

int i,j,length =
m_pChildren->data->num;

CCNode ** x =
(CCNode**)m_pChildren->data->arr;

CCNode *tempItem;

// insertion sort

for(i=1; i

{

tempItem =
x[i];

j =
i-1;

//continue
moving element downwards while zOrder is smaller or when zOrder is
the same but mutatedIndex is smaller

while(j>=0 && ( tempItem->m_nZOrder <
x[j]->m_nZOrder || ( tempItem->m_nZOrder== x[j]->m_nZOrder
&& tempItem->m_uOrderOfArrival <
x[j]->m_uOrderOfArrival ) ) )

{

x[j+1] = x[j];

j = j-1;

}

x[j+1] =
tempItem;

}

//don't need to check children recursively,
that's done in visit of each child

m_bReorderChildDirty = false;
}
}

就是一个选择排序,没有什么难的,只要熟悉就行了。

void CCNode::removeAllChildrenWithCleanup(bool cleanup)
{
// not using detachChild
improves speed here
if ( m_pChildren
&& m_pChildren->count() > 0 )
{

CCObject* child;

CCARRAY_FOREACH(m_pChildren, child)

{

CCNode*
pNode = (CCNode*) child;

if
(pNode)

{

// IMPORTANT:

// -1st do
onExit

// -2nd
cleanup

if(m_bRunning)

{

pNode->onExitTransitionDidStart();

pNode->onExit();

}

if (cleanup)

{

pNode->cleanup();

}

// set parent nil at the
end

pNode->setParent(NULL);

}

}

m_pChildren->removeAllObjects();
}

}

先是开始onExitTransitionDidStart,执行完成之后在执行onExit。在每个节点的退场都会执行这系列代码

一般清理掉CCNode的一些动作时,都会有事件派发出来,比如:提示脚本引擎

void CCNode::cleanup()
{
// actions

this->stopAllActions();

this->unscheduleAllSelectors();

if ( m_eScriptType !=
kScriptTypeNone)
{

CCScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this,
kCCNodeOnCleanup);
}

// timers

arrayMakeObjectsPerformSelector(m_pChildren, cleanup,
CCNode*);
}

实际上,如果在脚本中注册监听kCCNodeOnCleanup并绑定至相关函数,即可收到这个事件消息。如果要自定义一些事件(是事件,不是响应函数,分清楚哦),这需要我们在quick的
tolua文件中增加相关的调用接口。幸好,quick的源码好像也是可见,有需要的就可以改。这里再看看这句用法:

// timers

arrayMakeObjectsPerformSelector(m_pChildren, cleanup,
CCNode*);

#define arrayMakeObjectsPerformSelector(pArray, func,
elementType) \
do {

\
if(pArray &&
pArray->count() > 0)

\
{

\

CCObject* child;

\

CCARRAY_FOREACH(pArray, child)

\

{

\

elementType pNode = (elementType) child;

\

if(pNode)

\

{

\

pNode->func();

\

}

\

}

\
}

\
}

\
while(false)

这是与函数指针平行的用法,这种用法在cocos2d-x中随处可见
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: