1、单独载入精灵对象

渲染效率低,浪费资源,不推荐用该方法。代码例如以下:注:代码仅仅需贴到HelloWorldScene.cpp中就可以。

//First,单独渲染每个精灵帧
auto sprite = Sprite::create("grossini_dance_01.png");
sprite->setPosition(Vec2(visibleSize.width/2,visibleSize.height/4*3));
addChild(sprite); auto animation = Animation::create();
char strName[50] = {0};
for(int i = 1; i <= 14; i++)
{
sprintf(strName, "grossini_dance_%02d.png",i);
animation->addSpriteFrameWithFile(strName);//将全部的精灵载入到animation
} animation->setDelayPerUnit(3.0f / 14);//3秒内播放14帧动画
animation->setRestoreOriginalFrame(true);//重头開始播放
sprite->runAction(RepeatForever::create(Animate::create(animation)));//运行动作

2、一次载入,使用精灵帧

效率和资源有提高,可是使用Rect截取精灵对象不方便。代码例如以下:
//Second,一次渲染
auto textTure = Director::getInstance()->getTextureCache()->addImage("dragon_animation.png");
SpriteFrame* frame0 = SpriteFrame::createWithTexture(textTure, Rect(132*0,132*0,132,200));//截取精灵帧
SpriteFrame* frame1 = SpriteFrame::createWithTexture(textTure, Rect(132*1,132*0,132,200));
SpriteFrame* frame2 = SpriteFrame::createWithTexture(textTure, Rect(132*2,132*0,132,200));
SpriteFrame* frame3 = SpriteFrame::createWithTexture(textTure, Rect(132*3,132*0,132,200));
SpriteFrame* frame4 = SpriteFrame::createWithTexture(textTure, Rect(132*0,132*1,132,200));
SpriteFrame* frame5 = SpriteFrame::createWithTexture(textTure, Rect(132*1,132*1,132,200)); Vector<SpriteFrame*> arr;//载入精灵帧
arr.pushBack(frame0);
arr.pushBack(frame1);
arr.pushBack(frame2);
arr.pushBack(frame3);
arr.pushBack(frame4);
arr.pushBack(frame5); auto sp = Sprite::createWithSpriteFrame(frame0);//用第一帧精灵对象。初始化精灵
sp->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
addChild(sp); auto animation1 = Animation::createWithSpriteFrames(arr,0.2f);//运行动作
sp->runAction(RepeatForever::create(Animate::create(animation1)));

3、使用TexturePacker打包精灵对象。帧载入

推荐使用该方法:1)打开TexturePacker工具。addSprite导入精灵对象。2)Data Format选择cocos2d。3)Texture format使用
PNG格式。Layout的Max Size W和H能够修改。可是尺寸是2的幂。4)Publish sprite sheet,打包。保存地址就是project的Resource就可以。代码例如以下:
//Third,first和second的集合,使用TexturePacker工具,将精灵对象打包
auto cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("animation.plist");//载入plist文件 auto sp1 = Sprite::createWithSpriteFrameName("grossini_dance_01.png");//使用第一帧精灵初始化对象,精灵对象的名字与plist中的名字一致
sp1->setPosition(Vec2(visibleSize.width/2,visibleSize.height/4));
addChild(sp1); Vector<SpriteFrame*> arr1;
char str[50] = {0};
for(int i = 1; i <= 14; i++)
{
sprintf(str, "grossini_dance_%02d.png",i);//将精灵帧载入
auto frame = cache->getSpriteFrameByName(str);
arr1.pushBack(frame);
}
//运行动作
auto animation2 = Animation::createWithSpriteFrames(arr1,0.1f);
sp1->runAction(RepeatForever::create(Animate::create(animation2)));

4、效果图


Cocos2d-x3.3beta0创建动画的3种方式的更多相关文章

  1. 0036 Java学习笔记-多线程-创建线程的三种方式

    创建线程 创建线程的三种方式: 继承java.lang.Thread 实现java.lang.Runnable接口 实现java.util.concurrent.Callable接口 所有的线程对象都 ...

  2. 【java并发】传统线程技术中创建线程的两种方式

    传统的线程技术中有两种创建线程的方式:一是继承Thread类,并重写run()方法:二是实现Runnable接口,覆盖接口中的run()方法,并把Runnable接口的实现扔给Thread.这两种方式 ...

  3. js学习-DOM之动态创建元素的三种方式、插入元素、onkeydown与onkeyup两个事件整理

    动态创建元素的三种方式: 第一种: Document.write(); <body> <input type="button" id="btn" ...

  4. 创建TabHost的两种方式的简单分析

    最近做了一个TabHost的界面,在做的过程中发现了一些问题,故和大家分享一下. 首先我的界面如下: 目前就我所知,创建TabHost有两种方式,第一种是继承TabActivity类,然后用getTa ...

  5. Java创建线程的第二种方式:实现runable接口

    /*需求:简单的卖票程序多个窗口买票 创建线程的第二种方式:实现runable接口 *//*步骤1.定义类实现Runable接口2.覆盖Runable接口中的run方法    将线程要运行的代码存放在 ...

  6. 创建线程的两种方式比较Thread VS Runnable

    1.首先来说说创建线程的两种方式 一种方式是继承Thread类,并重写run()方法 public class MyThread extends Thread{ @Override public vo ...

  7. Java中创建线程的两种方式

    创建线程的第一种方式: 创建一个类继承Thread 重写Thread中的run方法 (创建线程是为了执行任务 任务代码必须有存储位置,run方法就是任务代码的存储位置.) 创建子类对象,其实就是在创建 ...

  8. javascript创建类的6种方式

    javascript创建类的7种方式 一 使用字面量创建 1.1 示例 var obj={}; 1.2 使用场景 比较适用于临时构建一个对象,且不关注该对象的类型,只用于临时封装一次数据,且不适合代码 ...

  9. 创建控制器的3种方式、深入了解view的创建和加载顺序

    转载自:http://blog.csdn.net/weisubao/article/details/41012243 (1)创建控制器的3种方式 - (BOOL)application:(UIAppl ...

随机推荐

  1. Aspnet_Session

    cmd: aspnet_regsql.exe -ssadd -sstype c -d ZZCasSession -S 192.168.0.3 -U sa -P szhweb2010 <!--会话 ...

  2. B - Helpful Maths

    Problem description Xenia the beginner mathematician is a third year student at elementary school. S ...

  3. JavaScript alert()函数的使用方法

    这里向大家简单介绍一下JavaScript alert()函数的使用,alert--弹出消息对话框,并且alert消息对话框通常用于一些对用户的提示信息. JavaScript alert()函数 a ...

  4. SQLServer 使用变量动态行转列

    drop table #testcreate table #test(    id int identity(1,1) primary key,    bizDate varchar(50),    ...

  5. PCL:描述三维离散点的ROPS特征(Code)

    前言: 三维点云为三维欧式空间点的集合.对点云的形状描述若使用局部特征,则可分为两种:固定世界坐标系的局部描述和寻找局部主方向的局部描述,ROPS特征为寻找局部主方向的特征描述. 1.寻找主方向(对X ...

  6. iOS https 证书链获取

    - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)chall ...

  7. php常用字符串和例子

    //输出一个或多个字符串 //注:echo 不是一个函数(它是一个语言结构), 因此你不一定要使用小括号来指明参数,单引号,双引号都可以 $a = "admin1"; $b = & ...

  8. 【转载】Java 集合详解

    转载:https://www.cnblogs.com/ysocean/p/6555373.html 一.集合的由来 通常,我们的程序需要根据程序运行时才知道创建多少个对象.但若非程序运行,程序开发阶段 ...

  9. LINUX - .so 与 .a

    .a gcc -c test1.c test2.c(或者g++ -c test1.cpp test2.cpp  )---   .o ar -r libtest.a test1.o test2.o    ...

  10. eas之添加表格列宽自动调整设置

    设置表格整体宽度自动调整为所在panel的宽度 KDTable table=new KDTable(); table. setAutoResize (boolean); 注意:该功能在冻结功能启用后, ...