前端之家收集整理的这篇文章主要介绍了
cocos2dx 点击事件分析(4),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1、
添加单点触摸事件,单点触摸事件有优先级,bSwallowsTouches是否吞噬,高优先级的
会优先响应,而且可以吞噬事件,不让触摸事件往低优先级传递。
void CCTouchDispatcher::addTargetedDelegate(CCTouchDelegate *pDelegate,int nPriority,bool bSwallowsTouches)
{
CCTouchHandler *pHandler = CCTargetedTouchHandler::handlerWithDelegate(pDelegate,nPriority,bSwallowsTouches);
if (! m_bLocked)
{
forceAddHandler(pHandler,m_pTargetedHandlers);
}
else
{
/* If pHandler is contained in m_pHandlersToRemove,if so remove it from m_pHandlersToRemove and return.
* Refer issue #752(cocos2d-x)
*/
if (ccCArrayContainsValue(m_pHandlersToRemove,pDelegate))
{
ccCArrayRemoveValue(m_pHandlersToRemove,pDelegate);
return;
}
//CCArray* m_pHandlersToAdd这个可以理解为一个临时保存CCTouchHandler的地方,
//m_bLocked 如果这个值为true,这把CCTouchHandler先加入到m_pHandlersToAdd,
//然后在合适的时间,即CCTouchDispatcher::touches函数中,再把m_pHandlersToAdd数组里面
//CCTouchHandler加入到m_pTargetedHandlers或者m_pStandardHandlers中。
m_pHandlersToAdd->addObject(pHandler);
m_bToAdd = true;
}
}
-->>
//
// handlers management根据优先级进行排序,然后添加到单点触摸事件列表中,由此可知,
//值越小,优先级越高
//
void CCTouchDispatcher::forceAddHandler(CCTouchHandler *pHandler,CCArray *pArray)
{
unsigned int u = 0;
CCObject* pObj = NULL;
CCARRAY_FOREACH(pArray,pObj)
{
CCTouchHandler *h = (CCTouchHandler *)pObj;
if (h)
{
if (h->getPriority() < pHandler->getPriority())
{
++u;
}
if (h->getDelegate() == pHandler->getDelegate())
{
CCAssert(0,"");
return;
}
}
}
pArray->insertObject(pHandler,u);
}
2、移除:和加入类似
void CCTouchDispatcher::removeDelegate(CCTouchDelegate *pDelegate)
{
if (pDelegate == NULL)
{
return;
}
if (! m_bLocked)
{
forceRemoveDelegate(pDelegate);
}
else
{
/* If pHandler is contained in m_pHandlersToAdd,if so remove it from m_pHandlersToAdd and return.
* Refer issue #752(cocos2d-x)
*/
CCTouchHandler *pHandler = findHandler(m_pHandlersToAdd,pDelegate);
if (pHandler)
{
m_pHandlersToAdd->removeObject(pHandler);
return;
}
ccCArrayAppendValue(m_pHandlersToRemove,pDelegate);
m_bToRemove = true;
}
}
-->>
void CCTouchDispatcher::removeDelegate(CCTouchDelegate *pDelegate)
{
if (pDelegate == NULL)
{
return;
}
if (! m_bLocked)
{
forceRemoveDelegate(pDelegate);
}
else
{
/* If pHandler is contained in m_pHandlersToAdd,pDelegate);
m_bToRemove = true;
}
}
原文链接:https://www.f2er.com/cocos2dx/343702.html