转载请注明地址:http://www.52php.cn/article/p-nsewocfp-mg.html
CCTouchDispatcher中是这样分发触摸事件的:
- CCARRAY_FOREACH(m_pTargetedHandlers,pObj)
- {
- pHandler=(CCTargetedTouchHandler*)(pObj);
-
- if(!pHandler)
- break;
- }
-
- boolbClaimed=false;
- if(uIndex==CCTOUCHBEGAN)
- bClaimed=pHandler->getDelegate()->ccTouchBegan(pTouch,pEvent);
- if(bClaimed)
- pHandler->getClaimedTouches()->addObject(pTouch);
- }else
- if(pHandler->getClaimedTouches()->containsObject(pTouch))
- {
-
- bClaimed=true;
- switch(sHelper.m_type)
- caseCCTOUCHMOVED:
- pHandler->getDelegate()->ccTouchMoved(pTouch,pEvent);
- caseCCTOUCHENDED:
- pHandler->getDelegate()->ccTouchEnded(pTouch,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; list-style-position:outside!important"> pHandler->getClaimedTouches()->removeObject(pTouch);
- caseCCTOUCHCANCELLED:
- pHandler->getDelegate()->ccTouchCancelled(pTouch,108); list-style-type:decimal-leading-zero; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; list-style-position:outside!important"> }
- if(bClaimed&&pHandler->isSwallowsTouches())
- if(bNeedsMutableSet)
- pMutableTouches->removeObject(pTouch);
- break;
- }
然后如果调用这里的
setPriority,就会重新排序数组:
rearrangeHandlers
copy
voidCCTouchDispatcher::setPriority(intnPriority,CCTouchDelegate*pDelegate)
CCAssert(pDelegate!=NULL,"");
CCTouchHandler*handler=NULL;
handler=this->findHandler(pDelegate);
CCAssert(handler!=NULL,153); background-color:inherit; font-weight:bold">if(handler->getPriority()!=nPriority)
handler->setPriority(nPriority);
this->rearrangeHandlers(m_pTargetedHandlers);
this->rearrangeHandlers(m_pStandardHandlers);
}
但是在业务层调用的
setPriority是这样的:
copy
voidCCTouchHandler::setPriority(intnPriority)
m_nPriority=nPriority;
}
所以你会发现如果在初始化一个控件的时候调用
setPriority就会有效,而在运行中动态更改就无效,就是因为初始化后的下一帧调用(调用onEnter时,会注册touchdelegate)会根据当前priority将其注册进那个数组:
copy
voidCCTouchDispatcher::forceAddHandler(CCTouchHandler*pHandler,CCArray*pArray)
unsignedintu=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
解决:
//设置层级
CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher();
pDispatcher->removeDelegate(m_pCtiyGateBtn);
pDispatcher->addTargetedDelegate(m_pCtiyGateBtn,1000,true); (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|