#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__)
请原谅我可怜的英语~~~!std :: bind和CC_CALLBACK_(0〜3)
在Cocos2d-x 3.x,CC_CALLBACK Technology
头文件"ccMacros.h"
使用新ISO C++ standard std::bind
,例如:
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的参数。
帮帮我!非常感谢!
我很抱歉,我的发言是unclear.I必须重新声明我的困惑:比如:listener-> onTouchMoved = CC_CALLBACK_2(的HelloWorld :: ontouchmoved,这一点);正如我们所看到的,成员函数“ontouchmoved”和指针“this”被传递给CC_CALLBACK_2,“__selector__”为“HelloWorld :: ontouchmoved”,“this”为__target__,但是没有更多参数传递给CC_CALLBACK_2,但是,std ::占位符:_1和std :: placeholders :: _ 2在CC_CALLBACK_2中,所以我猜std :: placeholders :: _ 1和std :: placeholders :: _ 2是没用的。 – CountingStars
占位符是Touch * touch和Event *事件。 – Makalele
但是,除了“HelloWorld :: ontouchmoved”和“this”之外,没有其他参数传递给CC_CALLBACK_2。因此,std :: placeholder :: _ 1和std :: placeholder :: _ 2代表什么数据?为什么他们代表Touch * touch和Event *事件? – CountingStars