#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__)
请原谅我的英语不好~~~!
头文件"ccMacros.h"
中Cocos2d-x 3.x,CC_CALLBACK Technology
使用的是new ISO C++ standard std::bind
,例如:</p>
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...");
}
我发送这个函数"HelloWorld::ontouchmoved"
和指针"this"
,`"HelloWorld::ontouchmoved" 是 CC_CALLBACK_2 中的选择器,"this" 是 CC_CALLBACK_2 中的目标。但是为什么呢?我不再向 CC_CALLBACK_2 发送参数,但 CC_CALLBACK_2 的定义是:
#define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
placeholders_1 和 placehoders_2 没有绑定参数,它们有什么用呢?我猜他们绑定了HelloWorld::ontouchmoved的参数,但我不知道绑定HelloWorld::ontouchmoved参数的方法。</p>
帮帮我!非常感谢!