Can’t assign CC_CALLBACK_2 member function to setCompleteListener

I’m trying to set a member function as a callback for an spine::setCompleteListener.
When i use cocos2dx v3.9 it’s work well, but now i use cocos2dx v3.17 and i got this error.
here is my code
auto transition = spine::SkeletonAnimation::createWithFile(“Spine/misc/transition.json”, “Spine/misc/transition.txt”);
transition->setPosition(cocos2d::Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));
scene->addChild(transition, 100);
transition->setAnimation(0, “transition_enter”, false);
transition->setCompleteListener(CC_CALLBACK_2(PreGameShopScene::listenComplete, this));

The errors are all similar to this:
error: no viable conversion from ‘__bind<void (PreGameShopScene::*)(int, int), PreGameShopScene *, const std::__ndk1::placeholders::__ph<1> &, const std::__ndk1::placeholders::__ph<2> &>’ to ‘const spine::CompleteListener’ (aka ‘const function<void (spTrackEntry *)>’)

Would love some guidance!

Avoid using the callback macros like CC_CALLBACK_2, especially for callbacks that aren’t directly related to Cocos2d-x engine code.

Now, because you’re using the macro, it makes it harder to see the problem, unless you look at the definition of that macro:
#define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)

It’s expecting 2 parameters to be sent to the callback, but if you look at the declaration of setCompleteListener, you would note that it only requires 1 parameter, and it’s actually showing you this in the error message.

So, again, I suggest you avoid using the built in CC_CALLBACK_* macros, and if you can avoid it, don’t use std::bind either.

Just use lambdas:

transition->setCompleteListener([](spine::TrackEntry* entry) {
    // handle the callback here

At least now if you do get any error message regarding the callback, it’s much easier to spot any issues.


This is the answer.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.