cocos2d x中關於touch事件的響應

2021-09-23 19:33:47 字數 2121 閱讀 3750



一、

touch

事件響應分為單點觸控響應和多點觸控響應。

單點觸控響應需要過載的方法:

virtual boolcctouchbegan(cctouch *ptouch, ccevent *pevent);

virtual voidcctouchmoved(cctouch *ptouch, ccevent *pevent);

virtual voidcctouchended(cctouch *ptouch, ccevent *pevent);

virtual voidcctouchcancelled(cctouch *ptouch, ccevent *pevent);

多點觸控需要過載的方法:

virtual voidcctouchesbegan(ccset *ptouches, ccevent *pevent);

virtual void cctouchesmoved(ccset*ptouches, ccevent *pevent);

virtual voidcctouchesended(ccset *ptouches, ccevent *pevent);

virtual voidcctouchescancelled(ccset *ptouches, ccevent *pevent);

二、單點觸控本質上需要呼叫的方法,即設定

touch

的**ccdirector::shareddirector()->gettouchdispatcher()->addtargeteddelegate(this,-129,true);

多點觸控本質上需要呼叫的方法

ccdirector::shareddirector()->gettouchdispatcher()->addstandarddelegate(this,-129);

所以,只要呼叫了以上方法,就可以實現觸控響應,就不再需要呼叫

settouchenabled(true);

三、settouchenabled

的方法實現

void cclayer::settouchenabled(bool enabled)

else}}

}

這段**最主要的一句

this->registerwithtouchdispatcher();

這個方法中就是呼叫第二點中提到的兩個

delegate

新增方法。

所以,咱們實現

touch

響應的話,需要過載

registerwithtouchdispatcher()

方法,並在這個方法中新增實現

void helloworld::registerwithtouchdispatcher()

由此,實現

touch

事件響應需要做的操作:

1.過載

registerwithtouchdispatcher()

並實現2.settouchenabled(true);3.

過載touch

或者touches

系列方法

四、在cclayer

的registerwithtouchdispatcher()

中有關於

m_etouchmode

的判斷,並且有

settouchmode

()方法,引數有兩種:

kcctouchesonebyone

和kcctouchesallatonce

,分別代表單點觸控和多點觸控

當touchmode

為kcctouchesonebyone

時,呼叫的是

addtargeteddelegate

方法;當

touchmode

為kcctouchesallatonce

時,呼叫的是

addstandarddelegate

方法所以,「過載

registerwithtouchdispatcher()

並實現」可以由

settouchmode

()方法來替換

關於cocos2dx中tableView的一些理解

先看 h檔案中 ifndef helloworld scene h define helloworld scene h include cocos2d.h include cocos ext.h using ns cc using ns cc ext class helloworld public ...

cocos2dx筆記 關於action

1.基本動作instantaction 放置 place 隱藏 hide 顯示 show 可見切換 togglevisibility 2.延時動作 移動到 ccmoveto 移動 ccmoveby 跳躍到 ccjumpto 跳躍 ccjumpby 貝塞爾 ccbezierby 放大到 ccscale...

關於cocos2dx中動畫的整理

1.瞬時動作 ccactioninstant ccplace 用於定位 cchide ccshow 隱藏和顯示 cctogglevisibility 交替變化換是否為隱藏狀態 ccflipx ccflipy分別為水平翻轉和垂直翻轉 瞬時動畫 auto place place create ccp 5...