1. 程式人生 > >Cocos2d-x-3.10 新增偵聽事件

Cocos2d-x-3.10 新增偵聽事件

  首先,標頭檔案裡私有部分,宣告函式:

private:

//事件響應部分
bool onTouchBegan(Touch*, Event*);

void onTouchMoved(Touch*, Event*);

然後,在對應的.cpp檔案裡,呼叫

auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(jewelsGrid::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(jewelsGrid::onTouchMoved, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);//開啟偵聽


接下來就是具體補充了:

bool jewelsGrid::onTouchBegan(Touch* pTouch, Event* pEvent){
auto pos = this->convertToNodeSpace(pTouch->getLocation());

}

void jewelsGrid::onTouchMoved(Touch* pTouch, Event* pEvent){

//觸控點的位置
auto pos = this->convertToNodeSpace(pTouch->getLocation());

}

感覺跟js相差不多,最多語法上面的,,,

由於是三消遊戲,,在進行交換的時候,不能點選,此時要關閉偵聽事件:

_eventDispatcher->pauseEventListenersForTarget(this);//交換開始,關閉觸控監聽