cocos2d-x 动画加速与减速

cocos2d-x 动画加速与减速,第1张

概述转自:http://novacreo.com/%E7%A8%8B%E5%BA%8F%E7%BB%84/cocos2d-x%E5%8A%A8%E7%94%BB%E5%8A%A0%E9%80%9F%E4%B8%8E%E5%87%8F%E9%80%9F/ 动画是游戏的必然要素之一,在整个游戏过程中,又有着加速减速动画的需求。以塔防为例子,布塔的时候希望能够将游戏减速,布好塔后,则希望能将游戏加速;当某

转自:http://novacreo.com/%E7%A8%8B%E5%BA%8F%E7%BB%84/cocos2d-x%E5%8A%A8%E7%94%BB%E5%8A%A0%E9%80%9F%E4%B8%8E%E5%87%8F%E9%80%9F/

动画是游戏的必然要素之一,在整个游戏过程中,又有着加速、减速动画的需求。以塔防为例子,布塔的时候希望能够将游戏减速,布好塔后,则希望能将游戏加速;当某个怪被冰冻后,移动速度减缓,而其他怪的移动速度不变。cocos2d-x引擎为我们提供了很强大的接口,下面就将我实验的过程复述一遍,也方便他人。

1)实现全局的加速、减速。

通过设置Scheduler的timeScale,可以实现全局的加、减速。代码非常简单:

CCScheduler* pScheduler = CCDirector::sharedDirector()->@H_301_10@getScheduler();pScheduler->setTimeScale(2.0f); //实现加速效果pScheduler->setTimeScale(0.5f);实现减速效果

2)实现对某个CCActionInterval动作的加速、减速

方法一:很容易想到的一个方法就是改变CCAnimation的delay unit。代码如下:

CCAnimation* pAnimation = CCAnimationCache::sharedAnimationCache()->@H_301_10@animationBy@R_419_6889@(“xxx”);pAnimation->setDelayUnit(pAnimation->getDelayUnit()*0.2f); 速度为原来的5倍

这个方法有一个缺点:改变了CCAnimationCache中这个animation的delay unit。也就是说以后即使再从CCAnimationCache中获取这个animation,其delay unit已经是原来的0.2倍了。

方法二:cocos2d-x提供了CCSpeed的类,可以实现动画速度的调节。用法如下:

CCActionInterval* pActionInterval = CCMoveto::create(5.0f,ccp(500.0f,100.0f@H_301_10@));CCSpeed* pSpeed= CCSpeed::create(pActionInterval,128); line-height:1.5!important">1.5f); 1.5倍速运行@H_301_10@CCSpeed* pSpeed1 = CCSpeed::create(pActionInterval,128); line-height:1.5!important">0.2f); 0.2倍速运行@H_301_10@pSprite->runAction(pSpeed);

注意,如果pSprite有已经运行的动作,要用pSprite->stopActionByTag()停掉之前的动作,不然两个动作就叠加到一起了。

很多时候你的主角的动作利用CCAction来实现,移动则是在update刷帧函数或者一些选择器的方法中进行的,那么为了让你的主角慢动作比较逼真,那么Himi建议不要使用scheduleUpdate函数,因为这个你无法修改每次调用update的时间默认都是每帧都调用,那么你应该自己定义一个选择器当刷逻辑的函数,这样就能配合CCSpeed实现逼真慢动作拉~

3)对某个CCFiniteTimeAction类型动作的加速、减速

大部分时候,一个游戏人物的动作并非由单一一个CCActionInterval类型的动作构成,而是一串动作连起来,构成一个Sequence。用CCSequence::create(…)创建的对象都是CCFinteTimeAction类型的,CCSpeed并不适用。在CCSpeed类的说明里,明确指出”Thisactioncan’tbeSequenceablebecauseitisnotanCCIntervalAction”。那对于Sequence就束手无策了吗?非也。cocos2d-x引擎自带例子中,schedulerTest给我们展示了如何控制某个sprite的scheduler的timescale。废话少说,直接看代码。

在classtwoSchedulers中定义了两个customer的scheduler和两个CCActionManager。

CCScheduler *@H_301_10@sched1;CCScheduler *@H_301_10@sched2;CCActionManager *@H_301_10@actionManager1;CCActionManager *actionManager2;

在onEnter函数中,分别对两个sprite设置customer的ActionManager.

CCScheduler *defaultScheduler = CCDirector::sharedDirector()->@H_301_10@getScheduler(); Create a new scheduler,and link it to the main schedulersched1 = new@H_301_10@ CCScheduler();defaultScheduler->scheduleUpdateForTarget(sched1,128); line-height:1.5!important">0,false@H_301_10@); Create a new ActionManager,and link it to the new scheudleractionManager1 = new@H_301_10@ CCActionManager();sched1->scheduleUpdateForTarget(actionManager1,0); line-height:1.5!important"> Replace the default ActionManager with the new one.pSprite1->setActionManager(actionManager1);

通过以上的代码,就可以通过改变sched1的timescale来改变pSprite1的动作的快慢了。有了这种方法,那么就可以放弃CCSpeed的那种方法了。

总结

以上是内存溢出为你收集整理的cocos2d-x 动画加速与减速全部内容,希望文章能够帮你解决cocos2d-x 动画加速与减速所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1052092.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存