I am using cocos2d-x for a while, there is a problem constantly boring me: I want delay executes a function such as "void f(int a)" by schedule. But for cocos2d-x, it can not pass a variable on stack:
int a = 10; // call f(a) in delay
Of course I can use CCInteger, CCxxx, but it was too painful to do it. It just a copy of cocos2d, really troublesome in cpp.
So, is there a easy way let it to executes a std::bind(f, a)?
You can try c++11 feature , here is the code by my self:
Define CallbackHelper Class
class CallbackHelper : public CCObject
{
private:
std::function<void ()> _func;
public:
static CallbackHelper* create(std::function<void ()> func) {
CallbackHelper* ret = new CallbackHelper();
if (ret) {
ret->autorelease();
} else {
CC_SAFE_DELETE(ret);
}
ret->_func = func;
return ret;
}
void run() {
_func();
}
};
Make CCCallFunC:
int a = 10;
CallbackHelper* callback = CallbackHelper::create([a](){
cout<<a<<endl;
});
CCCallFunc::create(callback, callfunc_selector(CallbackHelper::run));
Good Luck