2015-12-14 47 views
1
#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的参数。

帮帮我!非常感谢!

回答

0

我完全不明白你的问题,但为什么不使用lambda函数?:

auto listener = EventListenerTouchOneByOne::create(); 
listener->setSwallowTouches(true); 

listener->onTouchBegan = [&](Touch* touch, Event* event){ 

}; 

listener->onTouchMoved = [&](Touch* touch, Event* event){ 

}; 

listener->onTouchEnded = [&](Touch* touch, Event* event){ 

}; 

listener->onTouchCancelled = [&](Touch* touch, Event* event){ 

}; 

_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); 
+0

我很抱歉,我的发言是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

+0

占位符是Touch * touch和Event *事件。 – Makalele

+0

但是,除了“HelloWorld :: ontouchmoved”和“this”之外,没有其他参数传递给CC_CALLBACK_2。因此,std :: placeholder :: _ 1和std :: placeholder :: _ 2代表什么数据?为什么他们代表Touch * touch和Event *事件? – CountingStars

0

请阅读addEventListenerWithSceneGraphPriority功能的更多细节。触摸和事件添加在这一点上。

0

的std ::占位符的目的:: _ N是允许的std ::结合以产生具有相匹配的原函数的第N个参数的参数的函数。

,如果你想创建一个函数,参数少你可以附加价值,而不是占位符(环绕的原始功能)或重新排序或复制原始参数。您还可以绑定通过值或引用传入的所有变量。

,如果你想了解更多有关的所有疯狂的事情可以做,你要多一点阅读了关于性病::绑定。

http://en.cppreference.com/w/cpp/utility/functional/bind

+0

感谢您的帮助! – CountingStars