1、创建精灵框架缓存,并向其中添加相应的动画文件(plist),最后,通过动画集缓存生产动画

CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("animations/grossini.plist");
cache->addSpriteFramesWithFile("animations/grossini_gray.plist", "animations/grossini_gray.png");
CCSpriteBatchNode *spritebatch = CCSpriteBatchNode::create("animations/grossini.png");//用于批量生产精灵
CCArray* animFrames = CCArray::createWithCapacity();//动态生成数组,类似于vector
for(int i = ; i < ; i++)
{
//sprintf的作用是字符串格式化,主要功能是把格式化的数据写入某个字符串中。sprintf(str, “ani_conch_%d.png”, 1)后str的值就变成了:ani_conch_1.png
sprintf(str, "grossini_blue_%02d.png",i);//两位数字,不够的话,用零补全
CCSpriteFrame *frame = cache->spriteFrameByName(str);
animFrames->addObject(frame);
}
animation = CCAnimation::createWithSpriteFrames(animFrames, 0.2f); //创建动画集,切换时间为0.2s
// Add an animation to the Cache
CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "dance_blue"); // 把此动画集加入动画集缓存中,并命名为dance_blue
CCAnimationCache *animCache = CCAnimationCache::sharedAnimationCache(); //共享动画集缓存
CCAnimation *normal = animCache->animationByName("dance"); //获得动画集缓存
CCAnimate *animN = CCAnimate::create(normal); //创建动画

另外,CCString * str = CCString::createWithFormat("market_chipsLogo%d.png", idx);也可以实现替换数字的效果。

2、直接传入多个图片文件,生成动画

CCSprite *mainsprite=CCSprite::create("catBody1.png");
CCAnimation *animation=CCAnimation::create();
animation->addSpriteFrameWithFileName("catBody1.png");
animation->addSpriteFrameWithFileName("catBody2-4.png");
animation->addSpriteFrameWithFileName("catBody3.png");
animation->addSpriteFrameWithFileName("catBody2-4.png");
animation->setDelayPerUnit(0.1f);//设置动画的间隔时间
animation->setRestoreOriginalFrame(true);//是否返回第一帧
mainsprite->runAction(CCRepeatForever::create(CCAnimate::create(animation)));

3、直接传入一张大图,包含多个小图,生成动画

CCTexture2D *pTexture=CCTextureCache::sharedTextureCache()->addImage("hero.png");
CCSpriteFrame *frame0=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(,,,));
CCSpriteFrame *frame1=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(,,,));
CCSpriteFrame *frame2=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(,,,));
CCSpriteFrame *frame3=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(,,,));
CCArray *animFrames=CCArray::create();
CC_BREAK_IF(!animFrames);
animFrames->addObject(frame0);
animFrames->addObject(frame1);
animFrames->addObject(frame2);
animFrames->addObject(frame3);
CCAnimation *animation=CCAnimation::createWithSpriteFrames(animFrames,0.2f);
heroSprite0->runAction(CCRepeatForever::create(animate));

cocos2d-x创建精灵动画方式汇总的更多相关文章

  1. cocos3.0使用cocostudio动画帧结合地图对象键值创建精灵动画

    内容例如以下: #include "cocos2d.h" #include "cocostudio/CocoStudio.h" //精灵猫和其它精灵的tag t ...

  2. cocos2d-x创建精灵动画

    创建动画一般过程: 1.创建精灵框架缓存,并向其中添加相应的动画文件(plist),最后,通过动画集缓存生产动画 CCSpriteFrameCache *cache = CCSpriteFrameCa ...

  3. 精灵动画Animation对话框组成Idle动画的各精灵

    精灵动画Animation对话框组成Idle动画的各精灵 1.3  精灵动画 场景中已经添加了精灵,现在是时候让让它动起来了.读者也许已经从精灵图集中,各精灵的命名中看出来了,这个精灵一共有两种动画状 ...

  4. 网页小实验——用canvas生成精灵动画图片

    实验目标:借助canvas把一张国际象棋棋子图片转换为一组适用于WebGL渲染的精灵动画图片,不借助其他图片处理工具,不引用其他库只使用原生js实现. 初始图片如下: 一.图片分割 将初始图片分割为六 ...

  5. Cocos2d学习之路三(使用Zwoptex创建精灵表单和CCAnimate动画)

    创建精灵表单: 创建动画先要把图片整合到一个图片上然后生成plist文件: 方法下载Zwoptex软件:http://www.zwopple.com/zwoptex/ 然后打开选择 create ne ...

  6. 创建帧动画1 - xml方式

    废话不多说,先看东西   创建帧动画1 - xml方式 帧动画的创建方式主要以下2种: * 用xml创建动画: * 用代码创建动画:   本文内容主要关注 xml文件 创建帧动画的方式   xml文件 ...

  7. SpriteSheet精灵动画引擎

    SpriteSheet精灵动画引擎   本文介绍Flash中SpriteSheet精灵序列图与其它渲染方式的性能对比.SpriteSheet的原理及注意实现,最后实现了一个精灵序列图的渲染引擎.本文的 ...

  8. css精灵动画

    精灵动画的实现 CSS Sprites在国内很多人叫CSS精灵,其实这个技术不新鲜,原理就是:靠不断的切换图片让人感觉视觉上不断在变化,例如gif动画之类的效果 那么前端如何实现精灵效果? 传统的就是 ...

  9. Unity中的动画系统和Timeline(2) 按钮动画和2D精灵动画

    按钮动画 1 创建按钮后,按钮的Button组件中,Transition我们平时用的时Tint,这次选择Animation 选择Auto Generate Animation,创建一个按钮动画 2 后 ...

随机推荐

  1. libusb(.NET)开源项目使用小结

    更多细节请参考官方帮助文档 1,修改设备类型为自己的标识 InfWizard项目里,改掉资源文件LibUsb-Win32-LUDN.Driver.Resources. 原来的三处libusb-win3 ...

  2. Android响应式界面开发要点

    现在很多项目需要到达同一个Apk既可以在Phone上跑也尅在tablet上跑,即界面要适应不同尺寸和类型的需要而自动调整.这个即为响应式设计.在web开发商响应式设计已经是个常谈的内容了,而对于and ...

  3. Xamarin 的 MVVM 之 Caliburn.Micro

    约定 Caliburn.Micro 以下简称 CMXamarin.Form 以下简称 XF 摘要CM 当前已释出 3.0 beta 版https://github.com/Caliburn-Micro ...

  4. Ruby On Rails 常用的精品Gem汇总

    首先需要注明一点,本文是原创的并不是从其它地方转载.所有的数据是我从 GitHub 和 RubyGems 上码下来的,数据的截取时间就是本文的发布日期. RubyGems 的下载量可以看到在用这个 g ...

  5. 正确对待bug

    正确对待bug 2016-10-09 公众号:一只程序媛 以前我一直以为bug是代码的天敌,我以为好的程序媛写出来的代码是应该没有bug的,零bug是终极奋斗目标. 后来,看到一句话"上帝创 ...

  6. Dictionary使用

    /// <summary> /// 除去数组中的空值和签名参数并以字母a到z的顺序排序 /// </summary> /// <param name="dicA ...

  7. DDD 领域驱动设计-看我如何应对业务需求变化?

    tks: http://www.cnblogs.com/xishuai/p/3972802.html

  8. HTML5——播放器

    有了H5的Video,妈妈再也不用担心我没安Flash插件了 根据Video提供的方法和属性,简单练习了一下,不说废话,直接上图片和代码 <html><head><tit ...

  9. 集群之LVS(负载均衡)详解

    提高服务器响应能力的方法 scale on  在原有服务器的基础上进行升级或者直接换一台新的性能更高的服务器. scale out  横向扩展,将多台服务器并发向外响应客户端的请求.优点:成本低,扩展 ...

  10. WPF小知识,MessageBox的多种用法

    我们在程序中经常会用到MessageBox. 现将其常见用法总结如下: 1.MessageBox.Show("Hello~~~~"); 最简单的,只显示提示信息. 2.Messag ...