cocos2d x 3 x 觸控響應

2021-07-01 23:15:55 字數 1705 閱讀 3543

3.x需要自己註冊監聽事件。

有兩種方式,乙個是用c++的bind繫結自定義函式,cocos2d-x封裝了一下,提供了介面: cc_callback_數字,數字代表引數個數。

eventlistener需要兩個引數(

touch * 和 

event *),所以這裡使用的是cc_callback_2:

auto dispatcher = director::getinstance()->geteventdispatcher();

auto listener = eventlistenertouchonebyone::create();

listener->ontouchbegan = cc_callback_2(helloworld::ontouchbegan, this);

listener->ontouchmoved = cc_callback_2(helloworld::ontouchmoved, this);

listener->ontouchended = cc_callback_2(helloworld::ontouchended, this);

listener->ontouchcancelled = cc_callback_2(helloworld::ontouchcancelled, this);

listener->setswallowtouches(true); //不向下傳遞觸控

dispatcher->addeventlistenerwithscenegraphpriority(listener, this);

bool helloworld::ontouchbegan(touch *touch, event *unused_event)void helloworld::ontouchmoved(touch *touch, event *unused_event){}void helloworld::ontouchended(touch *touch, event *unused_event){}void helloworld::ontouchcancelled(touch *touch, event *unused_event){}

另一種方式是使用c++的lambda,[&]代表lambda內部使用外部變數的方式是引用:

auto dispatcher = director::getinstance()->geteventdispatcher();

auto listener = eventlistenertouchonebyone::create();

listener->ontouchbegan = [&](touch *touch, event *unused_event)->bool;

listener->ontouchmoved = [&](touch *touch, event *unused_event);

listener->ontouchended = [&](touch *touch, event *unused_event);

listener->ontouchcancelled = [&](touch *touch, event *unused_event);

listener->setswallowtouches(true); //不向下傳遞觸控

dispatcher->addeventlistenerwithscenegraphpriority(listener, this);

Cocos2dx 3 x多點觸控問題

首先,這並不是什麼教程。只是今天折騰了一天的乙個比較傻的問題。3.x的eventlistener想必各位已經都會了。toucheventallatonce是多點觸控,但幾乎沒什麼人用過。用法不難,但是很多人和我一樣卡在了無論怎麼搞touches的數量都只有一,換句話說,就是死活單點觸控。搜了很多論壇...

cocos2d x 3 x記憶體管理

記憶體管理有ref提供的4個方法 void retain 將該物件的引用計數器 1 void release 將該物件的引用計數器 1 ref autorelease 不改變物件的引用計數器值,將物件新增到自動釋放池,返回物件本身 unsigned int getreferencecount con...

Cocos2d X 3 x的具體配置詳解

當前最新為3.3 r9d windows x86.zip 注 推薦使用2.7.8版本,版本過高可能會出現問題 安裝python平台。一直按next安裝就好 如果上面一步配置成功的話,你在cmd中輸入 cocos 可以看到如下畫面 安裝visual studio,這個很簡單。建立乙個屬於你cocos2...