cocos2d-x -------之笔记篇 动画的实现
cocos2d-x 动画的实现
一、实现原理
动画的实现其实就是使用一个完整的动作图片集来实现动画,达到动态的效果
动画动作类(CCAnimate)是加载一个动画类来实现动作。
动画类(CCAnimation)加载一个精灵帧数组来构成一个动画,
CCAnimate函数:
static CCAnimate* create(CCanimation* pAnimation)
CCAnimation创建函数
CCAnimation* CCAnimation::createWithSpriteFrames(CCArray *frames, float delay/* = 0.0f*/)
参数:数组,间隔时间
二、动作实现步骤
1、首先需要加载一个纹理图片
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(“纹理图片");
然后能够得到该纹理的宽高信息,
2、创建一个精灵,一般都是使用该纹理图片中的某个图片来作为显示精灵,这样在执行动画动作的时候能够更加的协调
首先我们需要加载
使用上得到的纹理创建一个精灵
CCSpriteFrame* spriteFrame1 = CCSpriteFrame::createWithTextrue(text,CCRectmake(x,y,宽,高));
CCSprite* sprite = CCSprite::createWithSpriteFrame(spriteFrame1);
这样能够将纹理的某个小区域用来创建一个精灵
3、创建一个精灵帧数组
CCArray array = CCArray::create();
然后将要执行动作的图片集存放在该数组中
4、使用该数组创建一个动画类
CCAnimation* animation = CCAnimation::createWithSpriteFrames(array,0.1f);
5、创建动画动作
CCAnimate* animate = CCAnimate::create(animation);
最后精灵执行该动作
下面是自己测试的代码:
bool MyActiondonghua::init(){
if(!CCLayer::init())
return false;
CCSize size = CCDirector::sharedDirector()->getWinSize();
//1
CCTexture2D * texture = CCTextureCache::sharedTextureCache()->addImage("dongzuo.png");
float texturewidht = texture->getContentSize().width;
float textureheight = texture->getContentSize().height;
float pwidth = texturewidht /10;
float pheight = textureheight / 4;
//2
//得到纹理图片的第一个图片
CCSpriteFrame* spriteFrame = CCSpriteFrame::createWithTexture(texture,CCRectMake(0,0,pwidth,pheight));
//使用这个spriteFrame来创建一个精灵进行显示
CCSprite* sprite = CCSprite::createWithSpriteFrame(spriteFrame);
sprite->setPosition(ccp(size.width/2,size.height/2) );
sprite->retain();
this->addChild(sprite);
//3
//创建动画
CCArray* array = CCArray::create();
for(int i = 0; i <9 ; i++){
CCSpriteFrame* tmpspritefreme = CCSpriteFrame::createWithTexture(texture,CCRectMake(pwidth*i,pheight,pwidth,pheight));
array->addObject(tmpspritefreme);
}
//4
CCAnimation *animation = CCAnimation::createWithSpriteFrames(array,0.1f);
//5
CCAnimate* animate = CCAnimate::create(animation);
//6
sprite->runAction(CCRepeatForever::create(animate));
return true;
}
cocos2d-x -------之笔记篇 动画的实现的更多相关文章
- cocos2d-x -------之笔记篇 环境的安装
cocos2d-x -------之笔记篇 环境的安装 使用到的工具有VS2010 cygwin android-NDK eclipse android SDK 1.首先是android相关环境的安 ...
- WPF 精修篇 动画组TransformGroup
原文:WPF 精修篇 动画组TransformGroup 动画分组 TransformGroup 一个元素可能要有缩放 ScaleTransform和移动 TranslateTransform等多个效 ...
- iOS学习笔记10-UIView动画
上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...
- webgl学习笔记四-动画
写在前面 建议先阅读下前面我的三篇文章. webgl学习笔记一-绘图单点 webgl学习笔记二-绘图多点 webgl学习笔记三-平移旋转缩放 下面我们将讲解下如何让一个正方形动起来~不断擦除和重绘 ...
- 【笔记篇】C#笔记3
笔记目录:http://blog.csdn.net/enzymii/article/details/77169928 C#的接口有点意思,我们说过可以用来多重继承.. using System; na ...
- 【笔记篇】C#笔记1
返回目录:目录请戳这里~ 以后的C#笔记如果不出意外的话都是Win10 Professional + VS2015 Professional出的,(当然还有直接在编译框敲的所以能不能过编译我也不知道┑ ...
- 【Flutter 实战】17篇动画系列文章带你走进自定义动画
老孟导读:Flutter 动画系列文章分为三部分:基础原理和核心概念.系统动画组件.8篇自定义动画案例,共17篇. 动画核心概念 在开发App的过程中,自定义动画必不可少,Flutter 中想要自定义 ...
- Android笔记:动画
android:fromDegrees 起始的角度度数 android:toDegrees 结束的角度度数,负数表示逆时针,正数表示顺时针.如10圈则比android:fromDegrees大3600 ...
- 学习笔记-- android动画简述
android支持三种类型的动画: ·属性动画 一种补间动画,通过在目标对象的任何属性的两个值之间应用赠了变化,可以生成一种动画效果.这种动画可以用来生成各种效果,例如:改变视图的颜色.透明条.淡入 ...
随机推荐
- mysql 蠕虫复制
INSERT into user_info(version,create_user_count,create_pc_count) select version,create_user_count,cr ...
- iOS Dev (22) 文件、路径
iOS Dev (22) 文件.路径 作者:CSDN 大锐哥 博客:http://blog.csdn.net/prevention 沙箱 Sandbox 的路径 和其他很多应用平台一样,iOS 也限定 ...
- 【转】Mysql 根据时间戳按年月日分组统计
create_time时间格式 SELECT DATE_FORMAT(create_time,'%Y%u') weeks,COUNT(id) COUNT FROM role GROUP BY week ...
- cURL模拟POST方式提交数据
curl_post.php文件: 1 $url = 'http://localhost/test/curl_post_deal.php'; 2 3 $post_data = array( 4 'use ...
- vs2005 测试 lua环境
(1)添加文件核路径 (2)库文件路径 (3)main.cpp #include <stdio.h>#include <string.h> extern "C&quo ...
- Session和Cookie的关系
Session和Cookie关系 两者构建了web的回话数据 Cookie作为客户端的回话,Session为服务器端的 共同点: 都是1对1的,(一个客户一个独立的回话) 都以键值对的方式存储数据 都 ...
- python----slots属性安全类
问题:__slots__可以用于构造安全的类.如果一个类使用了__slots__那么它的属性就不在自由了. 下面举例说明: 1.自由属性. class person(object): def __in ...
- sudo apt-get update
要用apt-get这种方式安装LAMP时,最好先运行下面在命令升级自己的系统这样是为了更新源,而如果你找的源不好,可能安装LMAP失败.#sudo apt-get update 获得最近的软件包的列表 ...
- SpringDataRedis事务处理
public Long leftPush(V value) { return this.ops.leftPush(this.getKey(), value); } public Long leftPu ...
- Hibernate 1、Hello Hibernate
所使用到的jar 包: 1.创建实体类 public class User { private Integer id; private String name; private String addr ...