1.最最原始的方法,先创建动画帧,再创建动画打包(animation),再创建动画(animate)

第一步:

创建动画帧:CCSpriteFrame,依赖于原始的资源图片(xx.png,xx.jpg)

CCSpriteFrame *frame1=CCSpriteFrame::create("1.png");

CCSpriteFrame *frame2=CCSpriteFrame::create("2.png");

CCSpriteFrame *frame3=CCSprteFrame::create("3.png");

...

第二步:创建动画打包,CCAnimation,依赖于创建好的动画帧,CCSpriteFrame

CCAnimation *animation=CCAnimation::create();

animation->addSpriteFrame(frame1);

animation->addSpriteFrame(frame2);

animation->addSpriteFrame(frame3);

...

设置帧动画之间的播放间隔

animation->setDelayPerUnit(0.2);

设置帧动画循环播放的次数

animation->setLoops(5);//-1表示无限循环

第三步:创建真正的动画:animate,依赖于动画打包,CCAnimation

CCAnimate *animate=CCAnimate::create(animation);

执行动画:spr->runAction(animate);

//animation->addSpriteFrameWithFileName(CCString::createWithFormat("animation/p_2_0%d.png", i + 1)->getCString());//通过图片直接创建帧,这是对上面的一种简化,但是没法利用帧缓存,效率不高

第二种创建动画的方法:

使用帧动画缓存:CCSpriteFrameCache,主要是简化了从原始图片一帧一帧的加载到内存的步骤

第一步:创建图片帧缓存,依赖于打包好的xx.plist图片文件

CCSpriteFrameCache::sharedFrameCache()->addSpriteFramesWithFile("xx.plist");

第二步:将图片帧缓存中的图片帧通过循环加入到CCArray数组中(容器),需要先创建好容器

CCArray *array=CCArray::create();

for(int i=0;i<n;i++)

{

  CCString *str=CCString::createWithFormat("%d.png",i+1);

  CCSpriteFrame *frame=CCSpriteFrameCache::sharedFrameCache()->spriteFrameByName(str);//通过图片帧的名字从图片帧缓存中获得图片帧

  array->addObject(str);//将图片帧加入数组容器中

}

CCAnimation *animation=CCAnimation::createWithSpriteFrames(array);//通过图片帧数组来创建动画打包

animation->setDelayUnit(0.2);

animation->setLoops(-1);

CCAnimate *animate=CCAnimate::create(animation);

spr->runAction(animate);

第三种创建帧动画的方法:

不需要先加载到容器(CCArray)中存起来,直接加入帧动画打包中即可.

第一步:创建帧动画缓存CCSpriteFrameCache

CCSpriteFrameCache::sharedFrameCache()->addSpriteFramesWithFile(xx.plist);

CCAnimation *animation=CCAnimation::create();

for(int i=0;i<n;i++)

{

  CCString str=CCString::createWithFormat("%d",i++);

  animation->addSpriteFrame(CCSpriteCache::sharedFrameCache()->spriteFrameByName(str->getCstring()));

}

animation->setDelayUnit(0.2);

animation->setLoops(-1);

CCAnimate *animate=CCAnimate::create(animation);

spr->runAction(animate);

第三种方法是结合了第一种和第二种方法优点,省去了先获取图片帧放入容器中,再统一从CCArray中提取图片帧来创建动画打包的步骤.

cocos2dx中创建动画的三种方法的更多相关文章

  1. JAVA中创建线程的三种方法及比较

    JAVA中创建线程的方式有三种,各有优缺点,具体如下: 一.继承Thread类来创建线程 1.创建一个任务类,继承Thread线程类,因为Thread类已经实现了Runnable接口,然后重写run( ...

  2. Java中创建线程的三种方法以及区别

    Java使用Thread类代表线程,所有的线程对象都必须是Thread类或其子类的实例.Java可以用三种方式来创建线程,如下所示: 1)继承Thread类创建线程 2)实现Runnable接口创建线 ...

  3. java创建线程的三种方法

    这里不会贴代码,只是将创建线程的三种方法做个笼统的介绍,再根据源码添加上自己的分析. 通过三种方法可以创建java线程: 1.继承Thread类. 2.实现Runnable接口. 3.实现Callab ...

  4. Java中创建数组的几种方法

    Java中创建数组的几种方法 public static void main(String[] args) { //创建数组的第一种方法 int[] arr=new int[6]; int intVa ...

  5. 《Java多线程面试题》系列-创建线程的三种方法及其区别

    1. 创建线程的三种方法及其区别 1.1 继承Thread类 首先,定义Thread类的子类并重写run()方法: package com.zwwhnly.springbootaction.javab ...

  6. Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法(转)

    Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法http://www.cnblogs.com/zdz8207/archive/2013/02/27/android- ...

  7. Openerp 中打开 URL 的三种 方法

    来自:http://shine-it.net/index.php/topic,8013.0.html 最近总结了,Openerp 中打开 URL 的三种 方法: 一.在form view 添加 < ...

  8. mysql 中添加索引的三种方法

    原文:http://www.andyqian.com/2016/04/06/database/mysqleindex/ 在mysql中有多种索引,有普通索引,全文索引,唯一索引,多列索引,小伙伴们可以 ...

  9. jQuery中detach&&remove&&empty三种方法的区别

    jQuery中empty&&remove&&detach三种方法的区别 empty():移除指定元素内部的所有内容,但不包括它本身 remove():移除指定元素内部的 ...

随机推荐

  1. 第一个过滤器Filter

    过滤器实现Filter接口javax.servlet.Filter package com.henau.example; import java.io.IOException; import java ...

  2. hexo资源--theme等

    Hexo (https://github.com/hexojs/hexo) [3]hexo你的博客(http://ibruce.info/2013/11/22/hexo-your-blog/) [4] ...

  3. ueditor 单独图片上传 转载

    <body> <script type="text/javascript"> //这个是图片上传的,网上还有附件上传的 (function($) { var ...

  4. iOS定位 (一) 地图定位

    带地图的定位方法#import <CoreLocation/CoreLocation.h> #import <MapKit/MapKit.h> <MKMapViewDel ...

  5. JS运动学习笔记 -- 任意值的运动框架(高/宽度,背景颜色,文本内容,透明度等)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. ThinkPHP框架部署

    ThinkPHP框架部署 1.下载ThinkPHP框架 下载地址:http://www.thinkphp.cn/ ThinkPHP官方网站 下载ThinkPHP3.2.3完整版,下载后如下图所示: A ...

  7. wiegand 问题

    在向门控器发送信号的时候,播放声音和通过GPIO向wiegand发送信号的时候,由于wiegand的资源优先级别不够和声音的播放可能发生了冲突,有时向GPIO发送信号的时候,发送失败. static ...

  8. THREE.js代码备份——canvas - lines - colors(希尔伯特曲线3D、用HSL设置线颜色)

    <!DOCTYPE html> <html lang="en"> <head> <title>three.js canvas - l ...

  9. age

    #include<iostream> #include<math.h> #define pi 3.14 using namespace std; int main() { in ...

  10. 自动化测试平台CATP

    CATP:报文类工具,可以测试功能