ocos2dx 3.0 之前。
cocos2dx 提供了三个函数
[cpp]
view plain
copy
- CCCallFunc::actionWithTarget(this,callfunc_selector(MyClass::callBackAnim));
- CCCallFuncND::actionWithTarget(this, callfuncND_selector(MyClass::callBackAnim), (void*)mInt);
- CCCallFuncND::actionWithTarget(this, callfuncND_selector(MyClass::callBackAnim));
他们分别可以传递0个参数,1个参数,传递自身节点。
而在c++ 11 可变参数模版这个特性出来后。 他们被简化掉了成一个函数。。
下面这个方法是Sample中提供的传递自身的方法。调用CC_CALLBACK_1来实现。
[cpp]
view plain
copy
- grossini->runAction( Sequence::create(
- MoveBy::create(1, Point(150,0)),
- CallFuncN::create(CC_CALLBACK_1(LogicTest::bugMe,this)),
- NULL)
- );
- void LogicTest::bugMe(Node* node)
- {
- node->stopAllActions(); //After this stop next action not working, if remove this stop everything is working
- node->runAction(ScaleTo::create(2, 2));
- }
这里是CC_CALLBACK_1的定义
[cpp]
view plain
copy
- #define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
##__VA_ARGS 是表示可变参数
然后如果你想传递多个参数。 你可以这么使用
[cpp]
view plain
copy
- Object *arg1;
- Node *arg2;
- Sprite *arg3;
- grossini->runAction( Sequence::create(
- MoveBy::create(1, Point(150,0)),
- CallFuncN::create(CC_CALLBACK_1(LogicTest::bugMe,this,arg1,arg2,arg3)),
- NULL)
- );
- void LogicTest::bugMe(Node* node,Object * arg1,Object * arg2, Sprite* arg3)
- {
- //(Node *)arg2
- node->stopAllActions(); //After this stop next action not working, if remove this stop everything is working
- node->runAction(ScaleTo::create(2, 2));
- }
但在调用CC_CALLBACK_1时, this指针是必须的。。
我们可以看到。。它总共传递了4个参数。 自身节点,对象1, 以及可以向上转型的Object 对象, 以及精灵对象 arg3
可变参数模版让一切都变的简单了。。