在Cocos2d-x中,CCTouchdispatcher类负责触摸事件的分发处理,其包含两种触摸事件standardDelegate(标准触摸事件)、targetedDelegate(目标触摸事件)
当调用setTouchEnabled(true)设置当前Layer接收触摸事件的时候,默认是开启标准触摸事件。
void cclayer::setTouchEnabled(bool enabled) { if (m_bTouchEnabled != enabled) { m_bTouchEnabled = enabled; if (m_bRunning) { if (enabled) { //注册触摸分发事件 this->registerWithTouchdispatcher(); } else { //移除分发事件 CCDirector::sharedDirector()->getTouchdispatcher()->removeDelegate(this); } } } }
void cclayer::registerWithTouchdispatcher() { CCTouchdispatcher* pdispatcher = CCDirector::sharedDirector()->getTouchdispatcher(); //使用lua脚本时的触摸事件注册 if (m_pScriptTouchHandlerEntry) { if (m_pScriptTouchHandlerEntry->isMultitouches()) { pdispatcher->addStandardDelegate(this,0); LUALOG("[LUA] Add multi-touches event handler: %d",m_pScriptTouchHandlerEntry->getHandler()); } else { pdispatcher->addTargetedDelegate(this,m_pScriptTouchHandlerEntry->getPriority(),m_pScriptTouchHandlerEntry->getSwallowstouches()); LUALOG("[LUA] Add touch event handler: %d",m_pScriptTouchHandlerEntry->getHandler()); } } else { //默认的触摸类型是kCCtouchesAllAtOnce,也就是多点触摸(标准触摸) if( m_etouchMode == kCCtouchesAllAtOnce ) { pdispatcher->addStandardDelegate(this,0); } else {//如果触摸类型是kCCtouchesOneByOne,则开启的是单点触摸(目标触摸) pdispatcher->addTargetedDelegate(this,m_nTouchPriority,true); } } }
//添加标准的触摸事件 void addStandardDelegate(CCTouchDelegate *pDelegate,int nPriority); //添加目标的触摸事件 void addTargetedDelegate(CCTouchDelegate *pDelegate,int nPriority,bool bSwallowstouches);
//标准触摸事件的回调函数,因为是多点触控,所以它所接收的参数是一个坐标的集合 virtual void cctouchesBegan(CCSet *ptouches,CCEvent *pEvent); virtual void cctouchesMoved(CCSet *ptouches,CCEvent *pEvent); virtual void cctouchesEnded(CCSet *ptouches,CCEvent *pEvent); virtual void cctouchesCancelled(CCSet *ptouches,CCEvent *pEvent); //目标触摸事件的回调函数,接收的是单个点的位置坐标 virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent); virtual void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent); virtual void ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent); virtual void ccTouchCancelled(CCTouch *pTouch,CCEvent *pEvent);
<span style="font-size:14px;color:#ff0000;"><strong>开启标准触摸事件的方式</strong></span>
bool HelloWorld::init() { if ( !cclayer::init() ) { return false; } //第一种 开启标准触摸事件的方式 this->setTouchEnabled(true); //第二种 开启标准触摸事件的方式 //this->registerWithTouchdispatcher(); } void HelloWorld::onExit() { //第一种 关闭标准触摸事件的方式 this->setTouchEnabled(false); //第二种 关闭标准触摸事件的方式 //CCDirector::sharedDirector()->getTouchdispatcher()->removeDelegate(this); }
<pre name="code" class="html">//获取标准触摸事件的触摸点 void TestController::cctouchesBegan(CCSet *ptouches,CCEvent *pEvent) { CCSetIterator it = ptouches->begin(); CCTouch* touch = (CCTouch*)(*it); CCPoint beginPos = touch->getLocation(); }
//标准触摸事件的回调void HelloWorld::cctouchesBegan(CCSet *ptouches,CCEvent *pEvent){}void HelloWorld::cctouchesEnded(CCSet *ptouches,CCEvent *pEvent){}void HelloWorld::cctouchesCancelled(CCSet *ptouches,CCEvent *pEvent){}void HelloWorld::cctouchesMoved(CCSet *ptouches,CCEvent *pEvent){}
开启目标触摸事件的方式
bool HelloWorld::init() { if ( !cclayer::init() ) { return false; } //第一种 开启目标触摸事件的方式 this->setTouchEnabled(true); this->setTouchMode(kCCtouchesOneByOne); //第二种 开启目标触摸事件的方式 //CCDirector::sharedDirector()->getTouchdispatcher()->addTargetedDelegate(this,-1,true); } void HelloWorld::onExit() { //第一种 关闭目标触摸事件的方式 this->setTouchEnabled(false); //第二种 关闭目标触摸事件的方式 //CCDirector::sharedDirector()->getTouchdispatcher()->removeDelegate(this); } //目标触摸事件回调 bool HelloWorld::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent){return true;} void HelloWorld::ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent){} void HelloWorld::ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent){} void HelloWorld::ccTouchCancelled(CCTouch *pTouch,CCEvent *pEvent){}
</pre><pre name="code" class="cpp" style="margin-top: 0px; margin-bottom: 10px; font-size: 13px; line-height: 24px; background-color: rgb(255,255);">