#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__)
Please forgive my poor English~~~!
The header file "ccMacros.h"
inCocos2d-x 3.x,CC_CALLBACK Technology
use the the new ISO C++ standard std::bind
, for instance:
class HelloWorldScene : cocos2d::Layer
{
public :
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(HelloWorldScene);
// overload this function
void ontouchmoved(cocos2d::Touch*, cocos2d::Event*);
};
// HelloWorld.cpp
bool HelloWorldScene::init()
{
auto listener = cocos2d::EventListenerTouchOneByOne::create();
listener->onTouchBegan = [](cocos2d::Touch* _touch, cocos2d::Event* _event){ CCLOG("onTouchBegan..."); return true;};
// using the CC_CALLBACK
listener->onTouchMoved = CC_CALLBACK_2(HelloWorldScene::ontouchmoved, this);
listener->onTouchEnded = [](cocos2d::Touch* _touch, cocos2d::Event* _event){ CCLOG("onTouchEnded...");};
cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
void HelloWorldScene::ontouchmoved(cocos2d::Touch* touch, cocos2d::Event* event)
{
CCLOG("onTouchMoved...");
}
I send this function "HelloWorld::ontouchmoved"
and pointer "this"
, `"HelloWorld::ontouchmoved" is selector in CC_CALLBACK_2, "this" is target in CC_CALLBACK_2。
but why? I send no more parameter to CC_CALLBACK_2, but the definition of CC_CALLBACK_2 is :
#define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
The placeholders_1 and placehoders_2 bind no parameters, What's the use of them? I guess them bind the parameters of HelloWorld::ontouchmoved, but i don't kown the way to bind parameters of HelloWorld::ontouchmoved。
Help me!Thanks a lot!