cocos2dx 單點觸控,按鈕事件

2021-08-05 23:48:06 字數 1931 閱讀 3934

1,單點觸控:

.h:#include

using namespace std;

//觸控事件開始,手指按下時  

void ontouchesbegan(const std::vector& touches, cocos2d::event  *event);

//觸控移動事件,也就是手指在螢幕滑動的過程  

void ontouchesmoved(const std::vector& touches, cocos2d::event  *event);

//觸控事件結束,也就是手指鬆開時  

void ontouchesended(const std::vector& touches, cocos2d::event  *event); 

//打斷觸控事件,一般是系統層級的訊息,如來**,觸控事件就會被打斷  

void ontouchescancelled(const std::vector& touches, cocos2d::event  *event);

.cpp:

init()

void firstscene::ontouchesbegan(const std::vector& touches, cocos2d::event  *event)  

void firstscene::ontouchesmoved(const std::vector& touches, cocos2d::event  *event)  

void firstscene::ontouchesended(const std::vector& touches, cocos2d::event  *event)  

void firstscene::ontouchescancelled(const std::vector& touches, cocos2d::event  *event)  

2.按鈕事件:

#include "ui/uibutton.h"

using namespace ui;

using namespace cocos2d;

.h:void btntouchevent(cocos2d::ref *psender, toucheventtype type);

.cpp:

bool endscene::init()

this->scheduleupdate();

visibsize = director::getinstance()->getvisiblesize();

button*menubtn = button::create("res/menu.png");

menubtn->setposition(vec2(visibsize.width/2-50 , -100));

addchild(menubtn);

menubtn->setname("menubtn");

menubtn->addtoucheventlistener(this, toucheventselector(endscene::btntouchevent));

return true;

}void endscene::btntouchevent(cocos2d::ref *psender, toucheventtype type)

break;

}case cocos2d::ui::touch_event_moved:

log("touch_event_moved");

break;

case cocos2d::ui::touch_event_ended:

log("touch_event_ended");

break;

case cocos2d::ui::touch_event_canceled:

log("touch_event_canceled");

break;

default:

break;}}

cocos2dx 觸控相關記錄

簡單2句話 settouchenabled true 啟用觸控 ccdirector shareddirector gettouchdispatcher addtargeteddelegate this,0,true 新增觸控 優先順序值越小,越優先響應 同樣優先順序,後新增的先響應 多點觸控 ad...

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...