cocos2dx中创建动画的三种方法
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中创建动画的三种方法的更多相关文章
- JAVA中创建线程的三种方法及比较
JAVA中创建线程的方式有三种,各有优缺点,具体如下: 一.继承Thread类来创建线程 1.创建一个任务类,继承Thread线程类,因为Thread类已经实现了Runnable接口,然后重写run( ...
- Java中创建线程的三种方法以及区别
Java使用Thread类代表线程,所有的线程对象都必须是Thread类或其子类的实例.Java可以用三种方式来创建线程,如下所示: 1)继承Thread类创建线程 2)实现Runnable接口创建线 ...
- java创建线程的三种方法
这里不会贴代码,只是将创建线程的三种方法做个笼统的介绍,再根据源码添加上自己的分析. 通过三种方法可以创建java线程: 1.继承Thread类. 2.实现Runnable接口. 3.实现Callab ...
- Java中创建数组的几种方法
Java中创建数组的几种方法 public static void main(String[] args) { //创建数组的第一种方法 int[] arr=new int[6]; int intVa ...
- 《Java多线程面试题》系列-创建线程的三种方法及其区别
1. 创建线程的三种方法及其区别 1.1 继承Thread类 首先,定义Thread类的子类并重写run()方法: package com.zwwhnly.springbootaction.javab ...
- Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法(转)
Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法http://www.cnblogs.com/zdz8207/archive/2013/02/27/android- ...
- Openerp 中打开 URL 的三种 方法
来自:http://shine-it.net/index.php/topic,8013.0.html 最近总结了,Openerp 中打开 URL 的三种 方法: 一.在form view 添加 < ...
- mysql 中添加索引的三种方法
原文:http://www.andyqian.com/2016/04/06/database/mysqleindex/ 在mysql中有多种索引,有普通索引,全文索引,唯一索引,多列索引,小伙伴们可以 ...
- jQuery中detach&&remove&&empty三种方法的区别
jQuery中empty&&remove&&detach三种方法的区别 empty():移除指定元素内部的所有内容,但不包括它本身 remove():移除指定元素内部的 ...
随机推荐
- 自定义的UITabbar上面的按钮的x坐标的计算方法
; i < 4; i++) {//4是按钮的个数 NSString *backImage = backgroud[i]; NSString *heightImage = heightBackgr ...
- 10 Best TV Series Based On Hacking And Technology
Technology is rapidly becoming the key point in human lives. Here we have discussed top TV shows whi ...
- css3 transfrom使用以及其martix(矩阵)属性与其它属性的关系
写法 其属性martix与skew .scale .translate之间的关系 兼容性 : IE9+ : -ms-transform : IE9只支持2D转换 fire ...
- 关于SVN下载代码和Android Studio里面导入别人代码build tools不一致问题总结
项目build tools的sdk我没有 网络问题没法下载. 解决问题 方法 1.下载对应的SDK.需要VPN,或者FQ 2.将bulid.gradle的内容用下面的内容替换 // Top-level ...
- 配置Rip的认证
实验拓扑图 PS:我做实验使用的是DynamipsDUI模拟器,并且加载的是真实的ios镜像(c3745-advipservicesk9-mz.124-3c.bin) 1. 配置路由器R1 2. ...
- in/exists not in/not exists null
in/not in exists/not exists null的理解 两个测试表 create table tmp01 as with tmp as ( select '1' as id from ...
- 【Linux C中文函数手册】之 目录操作函数
目录操作函数 1)closedir 关闭目录 相关函数: opendir表头文件: #include<sys/types.h> #include<dirent.h>定义函数: ...
- SpringMvc入门三----控制器
在传统的Spring MVC开发方法中,必须在Bean配置文件中为每个控制器类配置实例和请求映射和让每个控制器类去实现或者扩展特定于框架的接口或者基类,不够灵活. 如果Spring MVC可以自动侦测 ...
- apache commons-email1.3使用
apache commons-email1.3下载地址: https://repository.apache.org/content/repositories/orgapachecommons-0 ...
- Codevs 3231 小吃
时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description 这里简直是吃货的天堂,小吃太多了.数不胜数. 假设岛上有N种小吃,每种体积Vi, ...