cocos2dx 觸控相關記錄

2021-06-22 16:18:36 字數 1372 閱讀 9633

簡單2句話:

settouchenabled(true);//啟用觸控

ccdirector::shareddirector()->gettouchdispatcher()->addtargeteddelegate(this, 0, true); //新增觸控

優先順序值越小,越優先響應

同樣優先順序,後新增的先響應

多點觸控:addstandarddelegate,對應touches系列函式

單點觸控:addtargeteddelegate,對應touch系列函式,另外,began必須重寫

原理: mswallowstouches = false,該層的touch事件若接受處理後,touch事件穿透,進入下個註冊touch事件的layer進行處理

mswallowstouches =true,當該層處理touch事件的時候,若cctouchbegan返回true時候,則touch事件被該層接收走,其他優先順序較低的,就不會接收到touch事件的處理申請了。

關於cctouchbegan的返回值

true:

本層的後續touch事件(move.cancel等)可以被觸發,並

阻擋向後層傳遞

false:

本層的後續touch事件

不能被觸發,並向後傳遞

具體應用:

1.如何阻塞事件的向後傳遞?

主要是利用了targetedtouchdelegate 的乙個叫swallowtouch的引數 ,如果這個開關開啟的話,比他優先順序低的handler 是收不到觸控響應的,優先順序參考之前的部分。

ccmenu 優先順序為-128,可以說是內部定義的最高優先順序,所以如果自定義需要更高的優先順序的話,可以比這個值再小。

mswallowstouches = true 並且 cctouchbegan 返回 true

2.如何讓layer所有觸控同時穿透begin、move、end事件?

mswallowstouches = false 並且 cctouchbegan 返回 true

cctouchbegan 返回 true 表示同層處理後續事件(吞噬)

cctouchbegan 返回 false 表示同層不處理後續事件(move end cancled)  (擊穿)

mswallowstouches 設為 true 表示觸控不向下層傳遞(不一定 如mswallowstouches為true began返回false還是會向後傳遞)

mswallowstouches 設為 false 表示觸控向下層傳遞(不知有啥用)

this->mtouchpriporty 越小,越先接收到觸控

this->mtouchpriporty 同等,越後addchild的越先響應

參考:

cocos2d x 觸控簡單分析

cclayer中的settouchenabled true 會開啟多點觸控。如果使用ccdirector shareddirector gettouchdispatcher addtargeteddelegate this,0,true 只會開啟乙個點的觸控。當helloworld cctouchb...

cocos2d x,螢幕觸控事情

auto listener eventlistenertouchonebyone create listener ontouchbegan touch touch,event event listener ontouchmoved touch touch,event event listener o...

cocos2d x之觸控的響應

1.boundingbox ccnode的乙個屬性,返回精靈的邊界。ccnode s attribute return the side of sprite 2.getcontentsize 每乙個精靈都被看成是乙個矩形,具有長和寬,單位是point,返回的是矩形的大小。every sprite i...