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里面去,这样程序只需要读取一次就行了,提高效率。
![](http://hi.csdn.net/attachment/201111/3/0_132032178337K4.gif)
比如我将这里的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后就很小了。用法一样。
猴子原创, 欢迎转载,转载请在明显处注明! 谢谢。
原文地址:http://blog.csdn.net/yanghuiliu/article/details/6933421
之前我介绍过cocos2d-x的帧动画实现,今天我把帧动画详细写一下。
帧动画就是很多张png的序列图实现轮流播放产生动画效果。
那么首先我们要一套动画的序列图,没有图的可以看引擎例子里面的图。很多张图我们可以采用TP工具将它们压缩到一张png里面去,这样程序只需要读取一次就行了,提高效率。
![](http://hi.csdn.net/attachment/201111/3/0_132032178337K4.gif)
比如我将这里的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后就很小了。用法一样。
相关文章推荐
- Android开发之图形图像与动画(二)Animation实现图像的渐变/缩放/位移/旋转
- 【cocos2d-x 手机游戏开发】Cocos2d-x 2.0 渲染到纹理深入分析
- Android 应用的动画实践--View Animation篇
- Qt-4.6动画Animation快速入门三字决
- LevelHelper Cocos2d-x with Box2d(二)处理碰撞事件
- 如何制作一个简单的游戏 Cocos2d-x 2.0.4
- 如何制作飞镖忍者(2) Cocos2d-x 2.0.4
- Cocos2d-x飞镖忍者(3)
- 如何制作一个基于Tile的游戏 Cocos2d-x 2.0.4
- 如何制作一个基于Tile的游戏(2) Cocos2d-x 2.0.4
- 如何制作一个横版格斗过关游戏(2) Cocos2d-x 2.0.4
- 如何制作一个塔防游戏 Cocos2d-x 2.0.4
- win7下搭建cocos2d-x androi开发环境不用cygwin的方法
- Cocos2d-x加Box2D制作弹弓类游戏
- eclipse下运行Android版cocos2d-x
- App 99.9%稳定 Crash原因
- cocos2d-x 手机屏幕多分辨率适应的问题。
- cocos2d-x 游戏开发----给单个精灵绑定点击事件
- cocos2d-x 给函数接口生命周期添加检测函数,用于调试程序时检测错误。