[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2014-01-26 标题cocos2dx学习笔记触摸响应分类编程 / C C / cocos2dx 标签cocos2dx·事件响应cocos2dx学习笔记触摸响应cocos2dx事件响应源码摘录CCLayer关于事件响应的代码CCTouchDispatcher最近项目中用到了cocos2dx来开发一个基于陀螺仪的demo忙了半个星期一边学习一边开发总算搞了出来。这里备份下开发中学习到的一些cocos2dx知识。cocos2dx事件响应源码摘录基于版本2.2.1代码版权属于cocos原作者。CCLayer关于事件响应的代码setTouchEnabledvoidCCLayer::setTouchEnabled(boolenabled){if(m_bTouchEnabled!enabled){m_bTouchEnabledenabled;if(m_bRunning){if(enabled){//注册事件响应this-registerWithTouchDispatcher();}else{//去除注册事件响应// have problems?CCDirector::sharedDirector()-getTouchDispatcher()-removeDelegate(this);}}}}registerWithTouchDispatchervoidCCLayer::registerWithTouchDispatcher(){CCTouchDispatcher*pDispatcherCCDirector::sharedDirector()-getTouchDispatcher();// Using LuaBindingsif(m_pScriptTouchHandlerEntry){//lua相关先略去。。。。。}else{//从此处可以发现TouchMode和TouchPriority可以决定注册事件响应的类型。//而Mode和Priority可以通过get/set设置。if(m_eTouchModekCCTouchesAllAtOnce){pDispatcher-addStandardDelegate(this,0);}else{pDispatcher-addTargetedDelegate(this,m_nTouchPriority,true);}}}//TouchMode的枚举定义如下:typedefenum{kCCTouchesAllAtOnce,kCCTouchesOneByOne,}ccTouchesMode;//get set 如下voidCCLayer::setTouchMode(ccTouchesMode mode){if(m_eTouchMode!mode){m_eTouchModemode;if(m_bTouchEnabled){setTouchEnabled(false);setTouchEnabled(true);}}}voidCCLayer::setTouchPriority(intpriority){if(m_nTouchPriority!priority){m_nTouchPrioritypriority;if(m_bTouchEnabled){setTouchEnabled(false);setTouchEnabled(true);}}}intCCLayer::getTouchPriority(){returnm_nTouchPriority;}intCCLayer::getTouchMode(){returnm_eTouchMode;}代码看到此处根据前文【 cocos2dx学习笔记吞噬事件相应 】提到的方法也可以使用下面代码//CCDirector::sharedDirector()-getTouchDispatcher()-addTargetedDelegate(this, -210, true);this-setTouchPriority(-210);this-setTouchMode(kCCTouchesOneByOne);this-setTouchEnabled(true);CCTouchDispatchervoidCCTouchDispatcher::touches(CCSet*pTouches,CCEvent*pEvent,unsignedintuIndex){CCAssert(uIndex0uIndex4,);CCSet*pMutableTouches;m_bLockedtrue;// optimization to prevent a mutable copy when it is not necessaryunsignedintuTargetedHandlersCountm_pTargetedHandlers-count();unsignedintuStandardHandlersCountm_pStandardHandlers-count();boolbNeedsMutableSet(uTargetedHandlersCountuStandardHandlersCount);pMutableTouches(bNeedsMutableSet?pTouches-mutableCopy():pTouches);structccTouchHandlerHelperDatasHelperm_sHandlerHelperData;//// process the target handlers 1st//if(uTargetedHandlersCount0){CCTouch*pTouch;CCSetIterator setIter;for(setIterpTouches-begin();setIter!pTouches-end();setIter){pTouch(CCTouch*)(*setIter);CCTargetedTouchHandler*pHandlerNULL;CCObject*pObjNULL;CCARRAY_FOREACH(m_pTargetedHandlers,pObj){pHandler(CCTargetedTouchHandler*)(pObj);if(!pHandler){break;}boolbClaimedfalse;if(uIndexCCTOUCHBEGAN){bClaimedpHandler-getDelegate()-ccTouchBegan(pTouch,pEvent);//如果该触摸began返回true说明handler开始处理这个事件但是不代表handler要吞噬这个事件if(bClaimed){pHandler-getClaimedTouches()-addObject(pTouch);}}else//经过测试发现当一个触摸事件生成之后从began,到end和cancel都是同一个对象。if(pHandler-getClaimedTouches()-containsObject(pTouch)){// moved ended canceledbClaimedtrue;switch(sHelper.m_type){caseCCTOUCHMOVED:pHandler-getDelegate()-ccTouchMoved(pTouch,pEvent);break;caseCCTOUCHENDED:pHandler-getDelegate()-ccTouchEnded(pTouch,pEvent);pHandler-getClaimedTouches()-removeObject(pTouch);break;caseCCTOUCHCANCELLED:pHandler-getDelegate()-ccTouchCancelled(pTouch,pEvent);pHandler-getClaimedTouches()-removeObject(pTouch);break;}}if(bClaimedpHandler-isSwallowsTouches()){if(bNeedsMutableSet){pMutableTouches-removeObject(pTouch);}//注意这里的break如果目标触摸被响应而且被设置吞噬那么直接跳出内层循环后续handler均不能响应这个触摸。break;}}}}//// process standard handlers 2nd////当目标触摸全部结束之后将挑剩下的touch事件一次性丢给标准触摸相应。if(uStandardHandlersCount0pMutableTouches-count()0){CCStandardTouchHandler*pHandlerNULL;CCObject*pObjNULL;CCARRAY_FOREACH(m_pStandardHandlers,pObj){pHandler(CCStandardTouchHandler*)(pObj);if(!pHandler){break;}switch(sHelper.m_type){caseCCTOUCHBEGAN:pHandler-getDelegate()-ccTouchesBegan(pMutableTouches,pEvent);break;caseCCTOUCHMOVED:pHandler-getDelegate()-ccTouchesMoved(pMutableTouches,pEvent);break;caseCCTOUCHENDED:pHandler-getDelegate()-ccTouchesEnded(pMutableTouches,pEvent);break;caseCCTOUCHCANCELLED:pHandler-getDelegate()-ccTouchesCancelled(pMutableTouches,pEvent);break;}}}if(bNeedsMutableSet){pMutableTouches-release();}//// Optimization. To prevent a which is expensive// the add/removes/quit is done after the iterations//m_bLockedfalse;。。。。。。 。。。。。。}