cocos2dx 对全部动作或单个动作速度的改变

转载自 http://blog.csdn.net/song_hui_xiang/article/details/27333051

一、对全部CCActionInterval动作进行加速或减速,这个很简单只要一句代码就可以。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. CCDirector::sharedDirector()->getScheduler()->setTimeScale(2);//以目前2倍速度播放  
  2.     CCDirector::sharedDirector()->getScheduler()->setTimeScale(0.5);//以目前0.5倍速度播放  

二、对个别单个动作进行加速或减速

有两种方法

1.通过CCSpeed实现

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. CCSprite* sprite = CCSprite::create();  
  2.     CCActionInterval *jump = CCJumpBy::create(4, ccp(0,0), 100, 4);  
  3.     sprite->runAction(CCSpeed::create(jump, 2));//以2倍速度播放动作  


2.通过CCActionManager实现,着重讲这个方法
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. CCScheduler* scheduler1;  
  2. void HelloWorld::myInit11()  
  3. {  
  4.     CCSize size = CCDirector::sharedDirector()->getWinSize();  
  5.       
  6.     CCControlSlider* slider1 = CCControlSlider::create("sliderTrack.png""sliderProgress.png""sliderThumb.png");  
  7.     slider1->setPosition(ccp(size.width/4*2, size.height*0.3));  
  8.     this->addChild(slider1);  
  9.     slider1->setTag(1);  
  10.     //最小值  
  11.     slider1->setMinimumValue(0);  
  12.     //最大值  
  13.     slider1->setMaximumValue(5);  
  14.     //回调函数  
  15.     slider1->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::sliderAction), CCControlEventValueChanged);  
  16.       
  17.       
  18.     // Create a new scheduler, and link it to the main scheduler  
  19.     scheduler1 = new CCScheduler();  
  20.     CCDirector::sharedDirector()->getScheduler()->scheduleUpdateForTarget(scheduler1, 0, false);  
  21.       
  22.     // Create a new ActionManager, and link it to the new scheudler  
  23.     CCActionManager* actionManager1 = new CCActionManager();  
  24.     scheduler1->scheduleUpdateForTarget(actionManager1, 0, false);  
  25.       
  26.       
  27.     //左边精灵  
  28.     CCActionInterval *jump1 = CCJumpBy::create(4, ccp(0,0), 100, 4);  
  29.     CCActionInterval *jump2 = jump1->reverse();  
  30.     CCSequence* seq = CCSequence::create(jump2, jump1, NULL);  
  31.     CCRepeatForever* action = CCRepeatForever::create(seq);  
  32.       
  33.     CCSprite* left = CCSprite::create("Icon.png");  
  34.     left->setPosition(ccp(size.width/4*1, size.height*0.5));  
  35.     this->addChild(left);  
  36.     // IMPORTANT: Set the actionManager running any action  
  37.     left->setActionManager(actionManager1);  
  38.     left->runAction(action);  
  39.       
  40.       
  41.     //中间精灵  
  42.     CCSprite* middle = CCSprite::create("Icon.png");  
  43.     middle->setPosition(ccp(size.width/4*2, size.height*0.5));  
  44.     this->addChild(middle);  
  45.     middle->setActionManager(actionManager1);  
  46.     middle->runAction(CCRepeatForever::create(CCRotateBy::create(2, 360)));  
  47.       
  48.       
  49.     //右边精灵  
  50.     CCSprite* right = CCSprite::create("Icon.png");  
  51.     right->setPosition(ccp(size.width/4*3, size.height*0.5));  
  52.     this->addChild(right);  
  53.     CCActionInterval* scale1 = CCScaleBy::create(1, 2);  
  54.     CCActionInterval* scale2 = scale1->reverse();  
  55.     CCSequence* seq2 = CCSequence::create(scale1, scale2, NULL);  
  56.     CCRepeatForever* action2 = CCRepeatForever::create(seq2);  
  57.     right->setActionManager(actionManager1);  
  58.     right->runAction(action2);  
  59. }  
  60.   
  61. void HelloWorld::sliderAction(CCObject* pSender, CCControlEvent controlEvent)  
  62. {  
  63.     CCControlSlider* pSliderCtl = (CCControlSlider*)pSender;  
  64.     float value = pSliderCtl->getValue();  
  65.       
  66.     scheduler1->setTimeScale(value);  
  67. }  
  68.   
  69. HelloWorld::~HelloWorld()  
  70. {  
  71.     scheduler1->release();  
  72. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值