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

前端之家收集整理的这篇文章主要介绍了cocos2dx 对全部动作或单个动作速度的改变前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

  1. CCDirector::sharedDirector()->getScheduler()->setTimeScale(2);//以目前2倍速度播放
  2. CCDirector::sharedDirector()->getScheduler()->setTimeScale(0.5);//以目前0.5倍速度播放

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

有两种方法

1.通过CCSpeed实现

  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实现,着重讲这个方法

  1. CCScheduler*scheduler1;
  2. voidHelloWorld::myInit11()
  3. {
  4. CCSizesize=CCDirector::sharedDirector()->getWinSize();
  5. CCControlSlider*slider1=CCControlSlider::create("sliderTrack.png","sliderProgress.png","sliderThumb.png");
  6. slider1->setPosition(ccp(size.width/4*2,size.height*0.3));
  7. this->addChild(slider1);
  8. slider1->setTag(1);
  9. //最小值
  10. slider1->setMinimumValue(0);
  11. //最大值
  12. slider1->setMaximumValue(5);
  13. //回调函数
  14. slider1->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::sliderAction),CCControlEventValueChanged);
  15. //Createanewscheduler,andlinkittothemainscheduler
  16. scheduler1=newCCScheduler();
  17. CCDirector::sharedDirector()->getScheduler()->scheduleUpdateForTarget(scheduler1,false);
  18. //CreateanewActionManager,andlinkittothenewscheudler
  19. CCActionManager*actionManager1=newCCActionManager();
  20. scheduler1->scheduleUpdateForTarget(actionManager1,false);
  21. //左边精灵
  22. CCActionInterval*jump1=CCJumpBy::create(4,4);
  23. CCActionInterval*jump2=jump1->reverse();
  24. CCSequence*seq=CCSequence::create(jump2,jump1,NULL);
  25. CCRepeatForever*action=CCRepeatForever::create(seq);
  26. CCSprite*left=CCSprite::create("Icon.png");
  27. left->setPosition(ccp(size.width/4*1,size.height*0.5));
  28. this->addChild(left);
  29. //IMPORTANT:SettheactionManagerrunninganyaction
  30. left->setActionManager(actionManager1);
  31. left->runAction(action);
  32. //中间精灵
  33. CCSprite*middle=CCSprite::create("Icon.png");
  34. middle->setPosition(ccp(size.width/4*2,size.height*0.5));
  35. this->addChild(middle);
  36. middle->setActionManager(actionManager1);
  37. middle->runAction(CCRepeatForever::create(CCRotateBy::create(2,360)));
  38. //右边精灵
  39. CCSprite*right=CCSprite::create("Icon.png");
  40. right->setPosition(ccp(size.width/4*3,size.height*0.5));
  41. this->addChild(right);
  42. CCActionInterval*scale1=CCScaleBy::create(1,2);
  43. CCActionInterval*scale2=scale1->reverse();
  44. CCSequence*seq2=CCSequence::create(scale1,scale2,NULL);
  45. CCRepeatForever*action2=CCRepeatForever::create(seq2);
  46. right->setActionManager(actionManager1);
  47. right->runAction(action2);
  48. }
  49. voidHelloWorld::sliderAction(CCObject*pSender,CCControlEventcontrolEvent)
  50. {
  51. CCControlSlider*pSliderCtl=(CCControlSlider*)pSender;
  52. floatvalue=pSliderCtl->getValue();
  53. scheduler1->setTimeScale(value);
  54. }
  55. HelloWorld::~HelloWorld()
  56. {
  57. scheduler1->release();
  58. }
原文链接:/cocos2dx/341370.html

猜你在找的Cocos2d-x相关文章