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. [BZOJ1271][WC2008]秦腾与教学评估(巧妙的二分)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1271 分析: 很巧妙的一道题 因为最多只有一个点是奇数,所以说明这个点前面的前缀和都是 ...

  2. linux中的进程管理

    USER  那个用户启动的进程 PID     该进程的ID号 %CPU   占用的CPU百分比 %MEM   占用的物理内存百分比 VSZ      占用虚拟内存大小 KB RSS      占用实 ...

  3. 第二课:判断js变量的类型以及domReady的原理

    1.类型的判断: js五种简单数据类型有:null,undefined,boolean,number,string. 还有复杂的数据类型:Object,Function,RegExp,Date,自定义 ...

  4. 实用的JS代码段(表单篇)

    整理了下比较实用的Javascript代码段,完整的代码参考 1 多个window.onload方法 由于onload方法时在页面加载完成后,自动调用的.因此被广泛的使用,但是弊端是只能实用onloa ...

  5. 利用less监视模式实时预览样式刷新浏览器

    [前言]此处介绍的方法只是我个人的用法,相信大家有更好更简洁的方式. 上次写到利用LiveReload解放F5.而且LiveReload可以编辑sass/less/stylus.但是可惜发现LiveR ...

  6. 过滤器-->GZIP压缩

    1.创建一个 可以使用GZIPOutputStream 压缩的流 package com.zh.yasuo2; import java.io.IOException; import java.util ...

  7. iOS不得姐项目--appearance的妙用,再一次设置导航栏返回按钮,导航栏左右按钮的封装(巧用分类)

    一.UI_APPEARANCE_SELECTOR 彩票项目中appearance的用法一直没有搞明白,这次通过第二个项目中老师的讲解,更深一层次的了解到了很多关于appearance的作用以及使用方法 ...

  8. C#中相对路径转换为绝对路径的方法

    第一种方法:使用System.Web类,System.Web.HttpContext.Current.Server.MapPath('相对路径');它还可以写成下面这种先声明空间,然后再使用函数的方式 ...

  9. Mysql常出现的问题

    1.mysql如何导入.txt文件?load data local infile 'D:\\data.txt' into table 表名 fields terminated by '\t';2.my ...

  10. BZOJ-1875 HH去散步 DP+矩阵乘法快速幂

    1875: [SDOI2009]HH去散步 Time Limit: 20 Sec Memory Limit: 64 MB Submit: 1196 Solved: 553 [Submit][Statu ...