Search code examples
c++cocos2d-x

What is the difference between all the CC_CALLBACK_# macros?


I'm new to cocos2dx and c++. I have been messing around with the starter HellowWorldScene and noticed this line:

auto closeItem = MenuItemImage::create(
                                       "CloseNormal.png",
                                       "CloseSelected.png",
                                       CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

When I change the CC_CALLBACK to CC_CALLBACK_2 xcode complains with "No matching function for call to 'create'". Why is that? What does the number at the end of CC_CALLBACK mean? Also can a function only accept one type of CC_CALLBACK selector?


Solution

  • This is in regard to the number of arguments that your callback function expects.

    From the docs :

    #define     CC_CALLBACK_0(__selector__, __target__,...)   std::bind(&__selector__,__target__, ##__VA_ARGS__)
    #define     CC_CALLBACK_1(__selector__, __target__,...)   std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
    #define     CC_CALLBACK_2(__selector__, __target__,...)   std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
    #define     CC_CALLBACK_3(__selector__, __target__,...)   std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, ##__VA_ARGS__)
    

    As you can see, versions with different numbers expand to different std::bind calls. You can read more about std::placeholders here : link

    Let me know if anything is not clear!