Wednesday, December 10, 2014

Cocos2D-X: EventListenerTouchOneByOne no viable overloaded '='

In Cocos2D-X version 3.0 and newer, there is a new way of handling touches (see example code below).

However the first time I used this, I encountered a compile error with message "EventListenerTouchOneByOne no viable overloaded '='" in lines 4-6 below. When this compile error happens, one reason is because of a wrong return type on their callbacks.
   EventListenerTouchOneByOne* touchListener = EventListenerTouchOneByOne::create();
   touchListener->setSwallowTouches(true);
   touchListener->onTouchBegan = CC_CALLBACK_2(MyLayer::touchBegan,this);
   touchListener->onTouchEnded = CC_CALLBACK_2(MyLayer::touchEnded,this);
   touchListener->onTouchCancelled = CC_CALLBACK_2(MyLayer::touchCancelled,this);
   touchListener->onTouchMoved = CC_CALLBACK_2(MyLayer::touchMoved,this);

   _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

The "onTouchBegan" callback has return type "bool". The rest need to have return type "void". Otherwise you will get the compile error above (I had all of them with return type "bool").
bool MyLayer::touchBegan(Touch *touch, Event *event) {
return true;
}
void MyLayer::touchEnded(Touch *touch, Event *event) {
}
void MyLayer::touchCancelled(Touch *touch, Event *event) {
}
void MyLayer::touchMoved(Touch *touch, Event *event) {
}

No comments:

Post a Comment