cocos2d-x动画加速与减速
动画是游戏的必定要素之中的一个,在整个游戏过程中,又有着加速、减速动画的需求。以塔防为样例,布塔的时候希望可以将游戏减速,布好塔后,则希望能将游戏加速;当某个怪被冰冻后,移动速度减缓,而其它怪的移动速度不变。cocos2d-x引擎为我们提供了非常强大的接口,以下就将我实验的过程复述一遍,也方便他人。
1)实现全局的加速、减速。
通过设置Scheduler的timeScale,能够实现全局的加、减速。代码很easy:
CCScheduler* pScheduler = CCDirector::sharedDirector()->getScheduler();
pScheduler->setTimeScale(2.0f); //实现加速效果
pScheduler->setTimeScale(0.5f);//实现减速效果
2)实现对某个CCActionInterval动作的加速、减速
方法一:非常easy想到的一个方法就是改变CCAnimation的delay unit。代码例如以下:
CCAnimation* pAnimation = CCAnimationCache::sharedAnimationCache()->animationByName(“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));
CCSpeed* pSpeed= CCSpeed::create(pActionInterval, 1.5f); //1.5倍速执行
CCSpeed* pSpeed1 = CCSpeed::create(pActionInterval, 0.2f);// 0.2倍速执行
pSprite->runAction(pSpeed);
注意,假设pSprite有已经执行的动作,要用pSprite->stopActionByTag()停掉之前的动作,不然两个动作就叠加到一起了。
—————————————————————–华丽丽的切割线————————————————————————–
来自HIMI的提示:
非常多时候你的主角的动作利用CCAction来实现,移动则是在update刷帧函数或者一些选择器的方法中进行的,那么为了让你的主角慢动作比較逼真,那么Himi建议不要使用scheduleUpdate函数,由于这个你无法改动每次调用update的时间默认都是每帧都调用,那么你应该自定义一个选择器当刷逻辑的函数,这样就能配合CCSpeed实现逼真慢动作拉~
3)对某个CCFiniteTimeAction类型动作的加速、减速
大部分时候,一个游戏人物的动作并不是由单一一个CCActionInterval类型的动作构成,而是一串动作连起来,构成一个Sequence。用CCSequence::create(…)创建的对象都是CCFinteTimeAction类型的,CCSpeed并不适用。在CCSpeed类的说明里,明白指出”This action can’t be Sequenceable because it is not an CCIntervalAction”。那对于Sequence就束手无策了吗?非也。cocos2d-x引擎自带样例中,schedulerTest给我们展示了怎样控制某个sprite的scheduler的timescale。废话少说,直接看代码。
在class TwoSchedulers中定义了两个customer的scheduler和两个CCActionManager。
CCScheduler *sched1;
CCScheduler *sched2;
CCActionManager *actionManager1;
CCActionManager *actionManager2;
在onEnter函数中,分别对两个sprite设置customer的ActionManager.
CCScheduler *defaultScheduler = CCDirector::sharedDirector()->getScheduler();
// Create a new scheduler, and link it to the main scheduler
sched1 = new CCScheduler();
defaultScheduler->scheduleUpdateForTarget(sched1, 0, false);
// Create a new ActionManager, and link it to the new scheudler
actionManager1 = new CCActionManager();
sched1->scheduleUpdateForTarget(actionManager1, 0, false);
// Replace the default ActionManager with the new one.
pSprite1->setActionManager(actionManager1);
通过以上的代码,就能够通过改变sched1的timescale来改变pSprite1的动作的快慢了。有了这样的方法,那么就能够放弃CCSpeed的那种方法了。
最新文章
- 如何使用查尔斯代理抓取https请求
- [转]java.lang.OutOfMemoryError:GC overhead limit exceeded
- Django url()函数详解
- json_encode时中文编码转正常状态
- Swift语法
- Discuz X3.2 SEO设置 title 不支持空格的解决方法
- TOJ 2732存钱计划(三)(单源最短路)
- 一些DevExpress控件概况!!!!主要DocumentManager.WindowsUIView.Tile
- 鼠标点击 input,显示瞬间的边框颜色,对之修改与隐藏
- OpenCV探索之路(十七):Mat和IplImage访问每个像素的方法总结
- tomcat内存溢出之PermGen space
- java学习笔记—集合之Map集合
- AWS Nginx Started but not Serving AWS上Nginx服务器无法正常工作
- MongoDB 3.4 安装以 Windows 服务方式运行
- poj1742 多维背包
- pycharm 如何设置方法调用字体颜色
- 使用FPM打包工具打rpm包
- [转]浅谈Android五大布局(一)——LinearLayout、FrameLayout和AbsoulteLayout
- js面试题-数组去重
- 题解 P2486 【[SDOI2011]染色】