给Layer添加触摸事件可以在init方法中添加以下代码:
auto touchlistener = EventListenerTouchOneByOne::create();
touchlistener->setSwallowTouches(true);//设置是否想下传递触摸
touchlistener->onTouchBegan = CC_CALLBACK_2(DirectionController::onTouchBegan, this);
touchlistener->onTouchMoved = CC_CALLBACK_2(DirectionController::onTouchMoved, this);
touchlistener->onTouchEnded = CC_CALLBACK_2(DirectionController::onTouchEnded, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(touchlistener, 1.0);
然后就可以实现一下各个方法了
bool DirectionController::onTouchBegan(Touch* pTouch, Event* pEvent) {
return true;
}
void DirectionController::onTouchMoved(Touch* pTouch, Event* pEvent) {
Point touchPoint = pTouch->getLocationInView();
Point touchGLPoint = Director::getInstance()->convertToGL(touchPoint);
}
void DirectionController::onTouchEnded(Touch* pTouch, Event* pEvent) {
}