转载自 http://blog.csdn.net/song_hui_xiang/article/details/27333051
一、对全部CCActionInterval动作进行加速或减速,这个很简单只要一句代码就可以。
- CCDirector::sharedDirector()->getScheduler()->setTimeScale(2);//以目前2倍速度播放
- CCDirector::sharedDirector()->getScheduler()->setTimeScale(0.5);//以目前0.5倍速度播放
二、对个别单个动作进行加速或减速
有两种方法
1.通过CCSpeed实现
- CCSprite* sprite = CCSprite::create();
- CCActionInterval *jump = CCJumpBy::create(4, ccp(0,0), 100, 4);
- sprite->runAction(CCSpeed::create(jump, 2));//以2倍速度播放动作
2.通过CCActionManager实现,着重讲这个方法
- CCScheduler* scheduler1;
- void HelloWorld::myInit11()
- {
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- CCControlSlider* slider1 = CCControlSlider::create("sliderTrack.png", "sliderProgress.png", "sliderThumb.png");
- slider1->setPosition(ccp(size.width/4*2, size.height*0.3));
- this->addChild(slider1);
- slider1->setTag(1);
- //最小值
- slider1->setMinimumValue(0);
- //最大值
- slider1->setMaximumValue(5);
- //回调函数
- slider1->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::sliderAction), CCControlEventValueChanged);
- // Create a new scheduler, and link it to the main scheduler
- scheduler1 = new CCScheduler();
- CCDirector::sharedDirector()->getScheduler()->scheduleUpdateForTarget(scheduler1, 0, false);
- // Create a new ActionManager, and link it to the new scheudler
- CCActionManager* actionManager1 = new CCActionManager();
- scheduler1->scheduleUpdateForTarget(actionManager1, 0, false);
- //左边精灵
- CCActionInterval *jump1 = CCJumpBy::create(4, ccp(0,0), 100, 4);
- CCActionInterval *jump2 = jump1->reverse();
- CCSequence* seq = CCSequence::create(jump2, jump1, NULL);
- CCRepeatForever* action = CCRepeatForever::create(seq);
- CCSprite* left = CCSprite::create("Icon.png");
- left->setPosition(ccp(size.width/4*1, size.height*0.5));
- this->addChild(left);
- // IMPORTANT: Set the actionManager running any action
- left->setActionManager(actionManager1);
- left->runAction(action);
- //中间精灵
- CCSprite* middle = CCSprite::create("Icon.png");
- middle->setPosition(ccp(size.width/4*2, size.height*0.5));
- this->addChild(middle);
- middle->setActionManager(actionManager1);
- middle->runAction(CCRepeatForever::create(CCRotateBy::create(2, 360)));
- //右边精灵
- CCSprite* right = CCSprite::create("Icon.png");
- right->setPosition(ccp(size.width/4*3, size.height*0.5));
- this->addChild(right);
- CCActionInterval* scale1 = CCScaleBy::create(1, 2);
- CCActionInterval* scale2 = scale1->reverse();
- CCSequence* seq2 = CCSequence::create(scale1, scale2, NULL);
- CCRepeatForever* action2 = CCRepeatForever::create(seq2);
- right->setActionManager(actionManager1);
- right->runAction(action2);
- }
- void HelloWorld::sliderAction(CCObject* pSender, CCControlEvent controlEvent)
- {
- CCControlSlider* pSliderCtl = (CCControlSlider*)pSender;
- float value = pSliderCtl->getValue();
- scheduler1->setTimeScale(value);
- }
- HelloWorld::~HelloWorld()
- {
- scheduler1->release();
- }