How to add touch event listener in Cocos2dx 3.1?
10,325
Solution 1
You should use CC_CALLBACK_2 macro.
Solution 2
Well, I usually subclass my button and add listeners to it. Example: http://cocos2d-x.org/wiki/How_To_Subclass_Sprite_And_Add_Event_Listeners
Author by
filip
Updated on July 16, 2022Comments
-
filip almost 2 years
I used following code to add listener method to a Button in cocos2dx:
button->addTouchEventListener(this, toucheventselector(HomeScene::doSomething));
But since Cocos2dx 3.1 both toucheventselector and addTouchEventListener is depreciated. There is :
void addTouchEventListener(ccWidgetTouchCallback callback);
I can't make it work - I tried every macro I could find - callfuncO_selector, CC_CALLBACK etc. I just don't know how to point to my method that looks like this:
void HomeScene::doSomething(Ref *pSender, Widget::TouchEventType type);
Failed to find any clue from Google, forum, git, change log, tests...
-
Narek almost 10 years@filip please write here more details. I have the same problem and don't know how to solve. I thought I should use
Widget::ccWidgetTouchCallback
? -
filip almost 10 yearsAs Lazar suggested, you should use CC_CALLBACK_2 macro instead of ccWidgetTouchCallback since there is no such thing yet in Cocos2dx 3.1 AFIK.
-
Narek almost 10 years@filip thank for your answer. Do you know if it is possible to set as a listener not a class method, but a lambda function?
-
null almost 10 years@Narek - Yes, you can use lambdas as callbacks:
EventListener* listener = EventListenerCustom::create(eventName, [this](EventCustom* e){ /* do something */ });