cocos2dxz之CCAnimationCache

通常情况下,对于一个精灵动画,每次创建时都需要加载精灵帧,按顺序添加到数组,再创建对应动作类,这是一个非常烦琐的计算过程。对于使用频率高的动画,比如鱼的游动,将其加入缓存可以有效降低每次创建的巨大消耗。由于这个类的目的和缓存内容都非常简单直接,所以其接口也是最简单明了的,如下所示:


[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** Retruns ths shared instance of the Animation cache */  
  2. static CCAnimationCache* sharedAnimationCache(void);  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** Adds a CCAnimation with a name. 
  2. */  
  3. void addAnimation(CCAnimation *animation, const char * name);  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** Returns a CCAnimation that was previously added. 
  2. If the name is not found it will return nil. 
  3. You should retain the returned copy if you are going to use it. 
  4. */  
  5. CCAnimation* animationByName(const char* name);  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** Deletes a CCAnimation from the cache. 
  2. */  
  3. void removeAnimationByName(const char* name);  

唯一不一样的是,这次动画缓存需要我们手动维护全部动画信息。也因为加载帧动画完全是代码操作的,目前还没有配置文件指导,所以不能像另外两个缓存那样透明化。实际上,如果考虑到两个场景间使用的动画基本不会重复,可以直接清理整个动画缓存。

所以,在场景切换时我们应该加入如下的清理缓存操作:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. void releaseCaches()  
  2. {  
  3.     CCAnimationCache::purgeSharedAnimationCache();  
  4.     CCSpriteFrameCache::sharedSpriteFrameCache()->removeUnusedSpriteFrames();  
  5.     CCTextureCache::sharedTextureCache()->removeUnusedTextures();  
  6. }  

值得注意的是清理的顺序,应该先清理动画缓存,然后清理精灵帧,最后是纹理。按照引用层级由高到低,以保证释放引用有效。


[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. CCAnimationCache *animationcache = CCAnimationCache::sharedAnimationCache();  
  2. animationcache->addAnimationsWithFile("animations/animations-2.plist");  
  3. CCAnimation *animation = animationcache->animationByName("dance_1");//刚开始不知道这个参数是哪里来的,其实是plist里面的,你用记事本打开就可以看得到,它是一个动画标识  
  4. animation->setRestoreOriginalFrame(true);  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值