Calling function of another class as touch callback?

Hi, I have a button in the class called “Shop”. And i’m using:

backButton->addTouchEventListener(CC_CALLBACK_2(HelloWorld::returnFromShop, this));

I’m using this code in Shop class, to call returnFromShop function when the button gets clicked. But returnFromShop() is in my main Class (HelloWorld). So it doesn’t work. the function needs to be in main class, and it needs to be non static. What can i do? thanks.

Hi, do you have pointer to HelloWorld in Shop class?
If no then pass it somehow and call needed function from your callback.

Another option to bind callback directly to HelloWorld function using pointer to it:

backButton->addTouchEventListener(CC_CALLBACK_2(HelloWorld::returnFromShop, helloWorldPointer));
2 Likes