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中随处可见
{
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中随处可见
相关文章推荐
- asterisk chan_sip.c代码分析(转…
- S3C6410的启动代码分析 一
- S3C6410的启动代码分析 一
- 微信公众平台&nbsp;示例代码&nbsp;分析
- hildon-desktop-1:2.0.7.debian.1.orig » src 中代码初步分析(2)
- Nand ECC校验和纠错原理及2.6.27内核ECC代码分析<转帖>
- <一>--RIL层代码分析-RIL_RadioFunctions *RIL_Init(funcs =rilInit()->mainloop()
- ecshop&nbsp;二次开发,源码分析
- [转]*PyQt4 精彩实例分析* 实例10 …
- Android Audio代码分析=>Stream=>Strateg优先级
- Intel 82599 ixgbe & ixgbevf CNA 卡驱动分析03——部分功能代码分析
- cocos2d-x CCNode,CCLayer,CCScene分析-沈大海cocos2d-x教程12
- Struts2/XWork < 2.2.0远程执行任意代码漏洞分析及修补[转自Neeao's Blog]
- 01010memcpy(&aa,cc,sizeof(AA));分析
- TQ2440裸奔程序>>2440init.s启动代码分析
- 反回上一页 代码
- hildon-desktop-1:2.0.7.debian.1.orig » src 中代码初步分析(1)
- 分析工具代码&转换跟踪代码
- Uchome1.2&nbsp;1.5&nbsp;代码学习&nbsp;common.ph…
- <四>--RIL层代码分析--整个电话来访过程