2dx CC_CALLBACK1选择器

来源:互联网 发布:mysql虚拟主机 编辑:程序博客网 时间:2024/05/18 09:09

ocos2dx  3.0 之前。 


cocos2dx 提供了三个函数

[cpp] view plaincopy
  1. CCCallFunc::actionWithTarget(this,callfunc_selector(MyClass::callBackAnim));    
  2.   
  3. CCCallFuncND::actionWithTarget(this, callfuncND_selector(MyClass::callBackAnim), (void*)mInt);  
  4.   
  5. CCCallFuncND::actionWithTarget(this, callfuncND_selector(MyClass::callBackAnim));  


他们分别可以传递0个参数,1个参数,传递自身节点。


而在c++ 11 可变参数模版这个特性出来后。  他们被简化掉了成一个函数。。


下面这个方法是Sample中提供的传递自身的方法。调用CC_CALLBACK_1来实现。

[cpp] view plaincopy
  1.    
  2. grossini->runAction( Sequence::create(   
  3.                                                 MoveBy::create(1, Point(150,0)),  
  4.                                                 CallFuncN::create(CC_CALLBACK_1(LogicTest::bugMe,this)),  
  5.                                                 NULL)   
  6.                         );  
  7.   
  8. void LogicTest::bugMe(Node* node)  
  9. {  
  10.     node->stopAllActions(); //After this stop next action not working, if remove this stop everything is working  
  11.     node->runAction(ScaleTo::create(2, 2));  
  12. }  


这里是CC_CALLBACK_1的定义

[cpp] view plaincopy
  1. #define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)  


##__VA_ARGS  是表示可变参数


然后如果你想传递多个参数。 你可以这么使用


[cpp] view plaincopy
  1.      Object *arg1;  
  2.      Node *arg2;  
  3.      Sprite *arg3;  
  4.      grossini->runAction( Sequence::create(   
  5.                                                 MoveBy::create(1, Point(150,0)),  
  6.                                                 CallFuncN::create(CC_CALLBACK_1(LogicTest::bugMe,this,arg1,arg2,arg3)),  
  7.                                                 NULL)   
  8.                         );  
  9.   
  10.   
  11. void LogicTest::bugMe(Node* node,Object * arg1,Object * arg2, Sprite* arg3)  
  12. {  
  13.     //(Node *)arg2  
  14.       
  15.     node->stopAllActions(); //After this stop next action not working, if remove this stop everything is working  
  16.     node->runAction(ScaleTo::create(2, 2));  
  17. }  


但在调用CC_CALLBACK_1时, this指针是必须的。。


我们可以看到。。它总共传递了4个参数。 自身节点,对象1,  以及可以向上转型的Object 对象,  以及精灵对象 arg3


可变参数模版让一切都变的简单了。。

0 0
原创粉丝点击