您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x -CCSpriteFrameCache-CCAnimation

2013-10-26 10:39 190 查看
转自:http://blog.csdn.net/cocos2der/article/details/6933421

猴子原创, 欢迎转载,转载请在明显处注明! 谢谢。

原文地址:http://blog.csdn.net/yanghuiliu/article/details/6933421

之前我介绍过cocos2d-x的帧动画实现,今天我把帧动画详细写一下。

帧动画就是很多张png的序列图实现轮流播放产生动画效果。

那么首先我们要一套动画的序列图,没有图的可以看引擎例子里面的图。很多张图我们可以采用TP工具将它们压缩到一张png里面去,这样程序只需要读取一次就行了,提高效率。



比如我将这里的6张图压成了一个png,TP会产生一个所有图的png和一个plist描述文件,plist很像xml,它描述了每一张图的位置,大小等信息。程序就是通过plist文件在合成的大png里面找到每一张图的。

合成的大图叫fei.png,对应的fei.plist。

里面的小图。叫 飞0001.png、飞0002.png、.........、飞0006.png

下面开始程序的创建

[cpp] view
plaincopy

        //创建cache  

CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();  

char strPlist[64] = {0};  

char strPng[64] = {0};  

sprintf(strPlist,"fei.plist");  

//sprintf(strPng,"fei.pvr.ccz");   

sprintf(strPng,"fei.png");  

cache->addSpriteFramesWithFile(strPlist, strPng);  

  

//创建动画每一帧,从cache里面读取  

CCMutableArray<CCSpriteFrame*>* animFrames = new CCMutableArray<CCSpriteFrame*>(6);  

  

char str[64] = {0};  

for(int i = 1; i <= 6; i++)   

{  

    sprintf(str, "飞%04d.png", i);  

    CCSpriteFrame* frame = cache->spriteFrameByName( str );  

    animFrames->addObject(frame);  

}  

  

CCAnimation* animation = CCAnimation::animationWithFrames(animFrames,0.04f);  

  

CCRepeatForever* mFly=CCRepeatForever::actionWithAction( CCAnimate::actionWithAnimation(animation, false));  

animFrames->release();  

cache->removeSpriteFramesFromFile(strPlist);  

这里的mFly就是一个创建好的action。当然它是可以重复播放的。你要是只想播放一次。

那么替换成

CCActionInterval* mFly=CCAnimate::actionWithAnimation(animation,true);

你可能看到了我里面注释了一行代码,就是我不是压缩成了png,而是压缩成了pvr.ccz。这种格式效率更高。

pvr是苹果自己支持的图片格式,但是比较占内存,压缩成ccz后就很小了。用法一样。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cocos2d-x animation