在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 ><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" >//获取标准触摸事件的触摸点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" >总结
以上是内存溢出为你收集整理的cocos2d-x的两种触摸事件全部内容,希望文章能够帮你解决cocos2d-x的两种触摸事件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)