转载请注明地址:http://www.jb51.cc/article/p-nsewocfp-mg.html
CCtouchdispatcher中是这样分发触摸事件的:
CCARRAY_FOREACH(m_pTargetedHandlers,pObj)//这里是遍历了一个注册过触摸事件的数组,按优先级排序 { pHandler = (CCTargetedtouchHandler *)(pObj); if (! pHandler) { break; } bool bClaimed = false; if (uIndex == CCtouchBEGAN) { bClaimed = pHandler->getDelegate()->cctouchBegan(ptouch,pEvent); if (bClaimed) { pHandler->getClaimedtouches()->addobject(ptouch); } } else if (pHandler->getClaimedtouches()->containsObject(ptouch)) { // moved ended canceled bClaimed = true; switch (sHelper.m_type) { case CCtouchmoveD: pHandler->getDelegate()->cctouchmoved(ptouch,pEvent); break; case CCtouchendED: pHandler->getDelegate()->cctouchended(ptouch,pEvent); pHandler->getClaimedtouches()->removeObject(ptouch); break; case CCtouchCANCELLED: pHandler->getDelegate()->cctouchCancelled(ptouch,pEvent); pHandler->getClaimedtouches()->removeObject(ptouch); break; } } if (bClaimed && pHandler->isSwallowstouches()) { if (bNeedsMutableSet) { pMutabletouches->removeObject(ptouch); } break; } }然后如果调用这里的 setPriority,就会重新排序数组: rearrangeHandlers
voID CCtouchdispatcher::setPriority(int nPriority,CCtouchDelegate *pDelegate){ CCAssert(pDelegate != NulL,""); CCtouchHandler *handler = NulL; handler = this->findHandler(pDelegate); CCAssert(handler != NulL,""); if (handler->getPriority() != nPriority) { handler->setPriority(nPriority); this->rearrangeHandlers(m_pTargetedHandlers); this->rearrangeHandlers(m_pStandardHandlers); }}
但是在业务层调用的 setPriority是这样的:
voID CCtouchHandler::setPriority(int nPriority){ m_nPriority = nPriority;}
所以你会发现如果在初始化一个控件的时候调用 setPriority就会有效,而在运行中动态更改就无效,就是因为初始化后的下一帧调用(调用onEnter时,会注册touchdelegate)会根据当前priority将其注册进那个数组:
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);}
所以,改变触摸分发数组的机会只有一次,之后如果想动态的改变优先级需要获取 touchdispatcher后设置,或者类似menu可以调用现成的函数 setHandlerPriority 总结
以上是内存溢出为你收集整理的cocos2d中 各种setTouchPriority无效的问题全部内容,希望文章能够帮你解决cocos2d中 各种setTouchPriority无效的问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)