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 -------之笔记篇 动画的实现的更多相关文章

  1. cocos2d-x -------之笔记篇 环境的安装

    cocos2d-x -------之笔记篇 环境的安装 使用到的工具有VS2010  cygwin android-NDK eclipse android SDK 1.首先是android相关环境的安 ...

  2. WPF 精修篇 动画组TransformGroup

    原文:WPF 精修篇 动画组TransformGroup 动画分组 TransformGroup 一个元素可能要有缩放 ScaleTransform和移动 TranslateTransform等多个效 ...

  3. iOS学习笔记10-UIView动画

    上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...

  4. webgl学习笔记四-动画

    写在前面 建议先阅读下前面我的三篇文章. webgl学习笔记一-绘图单点 webgl学习笔记二-绘图多点 webgl学习笔记三-平移旋转缩放   下面我们将讲解下如何让一个正方形动起来~不断擦除和重绘 ...

  5. 【笔记篇】C#笔记3

    笔记目录:http://blog.csdn.net/enzymii/article/details/77169928 C#的接口有点意思,我们说过可以用来多重继承.. using System; na ...

  6. 【笔记篇】C#笔记1

    返回目录:目录请戳这里~ 以后的C#笔记如果不出意外的话都是Win10 Professional + VS2015 Professional出的,(当然还有直接在编译框敲的所以能不能过编译我也不知道┑ ...

  7. 【Flutter 实战】17篇动画系列文章带你走进自定义动画

    老孟导读:Flutter 动画系列文章分为三部分:基础原理和核心概念.系统动画组件.8篇自定义动画案例,共17篇. 动画核心概念 在开发App的过程中,自定义动画必不可少,Flutter 中想要自定义 ...

  8. Android笔记:动画

    android:fromDegrees 起始的角度度数 android:toDegrees 结束的角度度数,负数表示逆时针,正数表示顺时针.如10圈则比android:fromDegrees大3600 ...

  9. 学习笔记-- android动画简述

    android支持三种类型的动画: ·属性动画  一种补间动画,通过在目标对象的任何属性的两个值之间应用赠了变化,可以生成一种动画效果.这种动画可以用来生成各种效果,例如:改变视图的颜色.透明条.淡入 ...

随机推荐

  1. PhoneGap Xcode iOS教程

    http://mobile.51cto.com/web-334924.htmhttp://phonegap.com/install/http://www.phonegap100.com/jiaoche ...

  2. @property属性

    1. 读写属性(readwrite/ readonly) 默认为readwrite,表示该属性既可以读取,也可以给该属性变量赋值:readonly则表示只能读取该属性变量. 2. 原子属性 (atom ...

  3. asp.net 两个页面之前传递数据

    .在两个表单之间传递数据 看下面的代码: 对于WebForm1: private void Page_Load(object sender, System.EventArgs e) { ArrayLi ...

  4. android 后台运行

    改写返回键事件监听,使得back键功能类似home键,让Acitivty退至后台时不被系统销毁,代码如下: public boolean onKeyDown(int keyCode, KeyEvent ...

  5. ExecutorService 的理解与使用

    ExecutorService 的理解与使用  http://my.oschina.net/bairrfhoinn/blog/177639 Java线程池ExecutorService http:// ...

  6. Pascal's Triangle,Pascal's Triangle II

    一.Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, giv ...

  7. jQuery 源码分析和使用心得 - 关于源码

    说到jQuery, 大家可能直觉的认为jQuery的源码应该就是一个jquery.xx.js这样的一个文件. 但是看到真正的源码的时候, 整个人都思密达了.jQuery的源码做的事远比你想象的多, 为 ...

  8. Get a handle on PHP Handlers

    PHP Handlers? mod_php? FPM? How do we make sense of the inner workings of PHP outside of our lines o ...

  9. jQuery validate (转载)

    转自:http://blog.sina.com.cn/s/blog_608475eb0100h3h1.html jQuery校验 官网地址:http://bassistance.de/jquery-p ...

  10. coroutine

    在脚本语言中,coroutine 不是个新鲜词汇,比如 lua 内建 coroutine,python中的greenlet,但在C程序中,并不是太常见. windows 下有 fiber,相关函数为 ...