2dx CC_CALLBACK1选择器

ocos2dx  3.0 之前。 


cocos2dx 提供了三个函数

[cpp]  view plain copy
  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 plain copy
  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 plain copy
  1. #define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)  


##__VA_ARGS  是表示可变参数


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


[cpp]  view plain copy
  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值