filip
filip

Reputation: 68

How to add touch event listener in Cocos2dx 3.1?

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

Upvotes: 4

Views: 10813

Answers (3)

SongYang
SongYang

Reputation: 13

You can also use this:

button->addTouchEventListener(CC_CALLBACK_2(HomeScene::doSomething,this)); 

to solve this issue :-)

Upvotes: 0

lazar89nis
lazar89nis

Reputation: 44

You should use CC_CALLBACK_2 macro.

Upvotes: 1

GameDeveloper
GameDeveloper

Reputation: 1029

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

Upvotes: 1

Related Questions