COCOS2D-X FRAME动画创建随笔

来源:互联网 发布:绝代双骄三 知乎 编辑:程序博客网 时间:2024/06/01 08:55


CCAnimate继承于CCActionInterval,及CCAnimate是一个action,具有action的所有属性及方法。

CCAnimate中的一些重要方法:

static CCAnimate* create ( CCAnimation * pAnimation )根据Animation创建Animate

virtual CCAnimation* getAnimation ( void ) 根据Animate获得Animation

bool initWithAnimation ( CCAnimation * pAnimation )根据Animation初始化只创建了Animate对象的实例

virtual CCActionInterval* reverse ( void )获得Animate对象反序对象

virtual void setAnimation ( CCAnimation * var )设置Animation

virtual void stop ( void ) 停止动作


由所有创建CCAnimate方法发现,所有的对象都是根据Animation来创建。

static CCAnimation * createWithSpriteFrames (CCArray *arrayOfSpriteFrameNames, float delay=0.0f)通过CCArray 和延时来创建CCAnimation 

CCArray 是一个CCSpriteFrame数组,表示该CCAnimation所有的动画帧,delay表示动画的延迟时间,每个动画帧间的时间间隔。

static CCArray* createWithCapacity ( unsigned int capacity )根据动画帧数创建数组容量,用于存放各个动画帧。

CCArray 中的CCSpriteFrame表示精灵动作的某帧,CCSpriteFrame的创建方法与使用精灵帧创建精灵一样。



动画创建中会使用到各种图片,若每次在加载图片时都进行处理,则会浪费很多系统资源,COCOS2D-X中通过CCTextureCache来解决这个问题。

CCTextureCache为纹理缓存类,该类是一个单实例类:

static CCTextureCache* sharedTextureCache ( )获得CCTextureCache单实例对象。

static void purgeSharedTextureCache ( ) 清理所有的缓存

void removeTexture ( CCTexture2D * texture )根据CCTexture2D 对象删除缓存

void removeTextureForKey ( const char * textureKeyName )根据CCTexture2D 对象名删除缓存

void removeUnusedTextures ( ) 清除掉所有没有再使用的缓存

CCTexture2D* addImage ( const char * fileimage )通过图片路径,将该图片纹理加入缓存,若该路径已经添加过则直接获取图片纹理对象。



当某一动画需要频繁使用时,同样使用动画缓存类来解决动画重复调用问题:

CCAnimationCache 用于管理动画缓存,该类为单实例。

static CCAnimationCache * sharedAnimationCache (void) 获取CCAnimationCache 单实例。

static void purgeSharedAnimationCache (void) 清理CCAnimationCache 类缓存空间

void addAnimation (CCAnimation *animation, const char *name) 向CCAnimationCache 中添加Animation对象,并以唯一表示字符串标识

void removeAnimationByName (const char *name)通过唯一标识字符串删除Animation对象

CCAnimation * animationByName (const char *name)通过唯一标识字符串获取已经添加的Animation对象

其他方法省略

所有缓存类的使用虽然提供了跟快的处理速度,但是牺牲了系统内存为代价,使用时应当择优使用


FRAME动画总结:动画是由具体精灵来展现,首先得到精灵的动作的所有动作帧 CCSpriteFrame,再通过CCArray 对象将所有的在同一动作中的动画帧组合起来。

通过这一系列的已经组合的动画帧创建出Animation对象,完成真正的动画。动画的执行需要动作来完成,所以创建CCAnimate动作来使精灵执行。


0 0