动画在游戏中是很常见的

程序1:创建一个简单的动画

首先须要在project文件夹下的Resource文件夹中放一张有各种不同动作的图片

在程序中加入以下的代码

#include "Animation.h"

CCScene* Animation::scene()
{
CCScene* s = CCScene::create(); Animation* layer = Animation::create(); s->addChild(layer); return s;
} bool Animation::init()
{
CCLayer::init(); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //创建精灵
CCSprite* sp = CCSprite::create();
sp->setPosition(ccp(winSize.width / 2, winSize.height / 2));
addChild(sp); //精灵放大4倍
sp->setScale(4); //创建纹理
CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("walkLeft.png"); //创建一个数组
CCArray* arr = CCArray::create(); //从纹理中扣了10帧frame,组成数组
for(int i = 0; i < 10; i++)
{
//使用纹理创建精灵帧
//第一个參数:纹理
//第二个參数:矩形
CCSpriteFrame* frame = CCSpriteFrame::createWithTexture(texture, //第一个參数:矩形的x坐标
//第二个參数:矩形的y坐标
//第三个參数:矩形的宽度
//第四个參数:矩形的高度
CCRect(i*18, 0, 18, 32)); arr->addObject(frame);
} //使用精灵帧创建动画
//第一个參数:数组
//第二个參数:动画的帧数(播放两张图片的间隔时间)
CCAnimation* animation = CCAnimation::createWithSpriteFrames(arr, 0.1f);
CCAnimate* animate = CCAnimate::create(animation); //播放动画
//CCRepeatForever::create动画播放无限次
sp->runAction(CCRepeatForever::create(animate)); return true;
}

运行结果:

程序2:有plist文件的动画载入

首先须要在project文件夹下的Resource文件夹中放一张有各种不同动作的图片和一个plist文件

plist格式的文件实际上是一个XML文件,文件里的内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>frames</key>
<dict>
<key>Boom_1.png</key>
<dict>
<key>frame</key>
<string>{{204,305},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_10.png</key>
<dict>
<key>frame</key>
<string>{{103,307},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_11.png</key>
<dict>
<key>frame</key>
<string>{{103,206},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_12.png</key>
<dict>
<key>frame</key>
<string>{{105,2},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_13.png</key>
<dict>
<key>frame</key>
<string>{{103,105},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_14.png</key>
<dict>
<key>frame</key>
<string>{{2,2},{101,101}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{101,101}}</string>
<key>sourceSize</key>
<string>{101,101}</string>
</dict>
<key>Boom_15.png</key>
<dict>
<key>frame</key>
<string>{{2,408},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_16.png</key>
<dict>
<key>frame</key>
<string>{{2,307},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_17.png</key>
<dict>
<key>frame</key>
<string>{{2,206},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_18.png</key>
<dict>
<key>frame</key>
<string>{{2,105},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_2.png</key>
<dict>
<key>frame</key>
<string>{{204,204},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_3.png</key>
<dict>
<key>frame</key>
<string>{{406,103},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_4.png</key>
<dict>
<key>frame</key>
<string>{{305,103},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_5.png</key>
<dict>
<key>frame</key>
<string>{{204,103},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_6.png</key>
<dict>
<key>frame</key>
<string>{{408,2},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_7.png</key>
<dict>
<key>frame</key>
<string>{{307,2},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_8.png</key>
<dict>
<key>frame</key>
<string>{{206,2},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
<key>Boom_9.png</key>
<dict>
<key>frame</key>
<string>{{103,408},{99,99}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{99,99}}</string>
<key>sourceSize</key>
<string>{99,99}</string>
</dict>
</dict>
<key>metadata</key>
<dict>
<key>format</key>
<integer>2</integer>
<key>realTextureFileName</key>
<string>PFBoom.png</string>
<key>size</key>
<string>{512,512}</string>
<key>textureFileName</key>
<string>PFBoom.png</string>
</dict>
</dict>
</plist>

程序代码:

#include "Animation.h"

CCScene* Animation::scene()
{
CCScene* s = CCScene::create(); Animation* layer = Animation::create(); s->addChild(layer); return s;
} //有plist文件的动画载入
bool Animation::init()
{
CCLayer::init(); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //创建精灵
CCSprite* sp = CCSprite::create();
sp->setPosition(ccp(winSize.width / 2, winSize.height / 2));
addChild(sp); //精灵放大4倍
sp->setScale(4); //创建精灵帧缓存
CCSpriteFrameCache* frameCache = CCSpriteFrameCache::sharedSpriteFrameCache(); //创建数组
CCArray* arr = CCArray::create(); //加入Plist文件
frameCache->addSpriteFramesWithFile("PFBoom.plist"); for (int i = 1; i <= 18; i++)
{
char key[128]; //通过名字获取精灵帧所在的图片
sprintf(key, "Boom_%d.png", i); //将图片加入到精灵帧中
CCSpriteFrame* frame = frameCache->spriteFrameByName(key); //将精灵帧加入到数组中
arr->addObject(frame);
} //使用数组创建动画
CCAnimation* animation = CCAnimation::createWithSpriteFrames(arr, 0.1f);
CCAnimate* animate = CCAnimate::create(animation); //动画消失
CCCallFunc* callfunc = CCCallFunc::create(sp, callfunc_selector(CCSprite::removeFromParent));
CCSequence* seq = CCSequence::create(animate, callfunc, NULL); //播放动画
sp->runAction(seq); return true;
}

运行结果:

程序3:通过鼠标实现动画切换

首先创建一个Animation类

在AnimationPreload.h中加入以下代码

#ifndef __AnimationPreload_H__
#define __AnimationPreload_H__ #include "cocos2d.h"
USING_NS_CC; class AnimationPreload : public CCLayer
{
public:
static CCScene* scene(); bool init(); CREATE_FUNC(AnimationPreload); bool ccTouchBegan(CCTouch*, CCEvent*); CCSprite* sp;
}; #endif

在AnimationPreload.cpp中加入以下的代码

#include "AnimationPreload.h"

CCScene* AnimationPreload::scene()
{
CCScene* s = CCScene::create(); AnimationPreload* layer = AnimationPreload::create(); s->addChild(layer); return s;
} bool AnimationPreload::init()
{
//chushihu
CCLayer::init(); //获取窗体的大小
CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //(创建纹理)从纹理里扣了10帧frame。组成数组
CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("walkLeft.png"); //创建数组
CCArray* arr = CCArray::create(); //retain for (int i = 9; i >= 0; i--)
{
//创建精灵帧
CCSpriteFrame* frame = CCSpriteFrame::createWithTexture(texture,
CCRect(i*18, 0, 18, 32)); //将精灵帧加入到数组中
arr->addObject(frame);
} // 通过数组创建动画
CCAnimation* animation = CCAnimation::createWithSpriteFrames(arr, 0.05f); // 加入到缓存
CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "MarioRun"); /***************************************************************/
/** 有plist文件的动画载入 **/ //创建精灵帧缓存
CCSpriteFrameCache* frameCache = CCSpriteFrameCache::sharedSpriteFrameCache(); //创建数组
arr = CCArray::create(); //将plist文件加入到精灵帧缓存中
frameCache->addSpriteFramesWithFile("PFBoom.plist"); for (int i = 1; i <= 18; i++)
{
char key[128]; //通过名字获取精灵帧所在的图片
sprintf(key, "Boom_%d.png", i); //将图片加入到精灵帧中
CCSpriteFrame* frame = frameCache->spriteFrameByName(key); //将精灵帧加入到数组中
arr->addObject(frame);
} //创建动画
animation = CCAnimation::createWithSpriteFrames(arr, 0.1f); // 加入到缓存
CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "PlaneBOOM"); sp = CCSprite::create();
sp->setPosition(ccp(winSize.width / 2, winSize.height / 2));
addChild(sp); //精灵方法4倍
sp->setScale(4); // setTouch
setTouchEnabled(true);
setTouchMode(kCCTouchesOneByOne); return true;
} bool AnimationPreload::ccTouchBegan(CCTouch*, CCEvent*)
{
sp->stopAllActions(); static int i = 1; if (i == 1)
{
sp->runAction(CCAnimate::create(
CCAnimationCache::sharedAnimationCache()->animationByName("PlaneBOOM")
));
}
else
{
sp->runAction(CCAnimate::create(
CCAnimationCache::sharedAnimationCache()->animationByName("MarioRun")
));
} i = 1 - i; return 0;
}

运行结果:




Cocos2d-X使用CCAnimation创建动画的更多相关文章

  1. cocos2dx中创建动画的三种方法

    1.最最原始的方法,先创建动画帧,再创建动画打包(animation),再创建动画(animate) 第一步: 创建动画帧:CCSpriteFrame,依赖于原始的资源图片(xx.png,xx.jpg ...

  2. wpf 创建动画三种方式

    动画类型 : 故事版,CompositionTarget,DispachTime 那么到此,三种动态创建动画的方法都已经详细介绍过了,大家可能会有种感觉,比较钟情于第一种WPF/Silverlight ...

  3. 《Programming WPF》翻译 第8章 5.创建动画过程

    原文:<Programming WPF>翻译 第8章 5.创建动画过程 所有在这章使用xaml举例说明的技术,都可以在代码中使用,正如你希望的.可是,代码可以使用动画在某种程度上不可能在x ...

  4. UWP 创建动画的极简方式 — LottieUWP

    提到 UWP 中创建动画,第一个想到的大多都是 StoryBoard.因为 UWP 和 WPF 的界面都是基于 XAML 语言的,所以实现 StoryBoard 会非常方便. 来看一个简单的 Stor ...

  5. WPF/WP/Silverlight/Metro App代码创建动画的思路

    在2010年之前,我都是用Blend创建动画,添加触发器实现自动动画,后来写成代码创建的方式.如今Blend已经集成到Visual Studio安装镜像中了,最新的VS2015安装,Blend的操作界 ...

  6. Cocos2D iOS之旅:如何写一个敲地鼠游戏(九):创建动画

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  7. Cocos2D 指定文件夹创建项目

    参考http://www.cnblogs.com/skynet/p/3428369.html 通过下面的一些改造,可以让新建的cocos2d-x项目独立于cocos2d-x引擎目录: 1)     将 ...

  8. [cocos2d] 利用texture atlases生成动画

    texturepacker可以方便地制作纹理贴图集(Texture Atlases),而且可以免费试用.(可在官网申请免费liscence) //利用软件将5帧png贴图生成1张大的png贴图和pli ...

  9. cocos2d-3.x 创建动画

    1.多文件帧序列动画 TrademarkAnimation.h #ifndef __TRADEMARK_ANIMATION_H__ #define __TRADEMARK_ANIMATION_H__ ...

随机推荐

  1. 验证Oracle处理速度

    (这是2009年写的东西了,在网上看到有人对数据库批量操作的'速度'比较关注,于是就把这篇老文章整理了一下) 一.环境及前提 在244上(一台稍好一些的机器,做了RAID,机械硬盘,Raid几忘了), ...

  2. 02.JSP内置对象

    一.内置对象:不再由用户进行实例化而可以直接使用的对象,一共九种,一定要清楚的记住每种内置对象的类型,以方便查询文档. 二.四种属性保存范围 1,  属性保存范围:指一个设置的对象,可以经过多少个其他 ...

  3. OpenCV二维Mat数组(二级指针)在CUDA中的使用

    CUDA用于并行计算非常方便,但是GPU与CPU之间的交互,比如传递参数等相对麻烦一些.在写CUDA核函数的时候形参往往会有很多个,动辄达到10-20个,如果能够在CPU中提前把数据组织好,比如使用二 ...

  4. 零基础如何迅速学习HTML5?新手小白学习web前端H5自白!

    很多的人在毕业之后才发现原来学的专业不是自己想做的工作,或者专业对口的工作待遇让人觉得并不满意,于是很多人选择培训机构学新的一门技能转换行业.IT行业的web前端H5受到很多学员的青睐.那么学习web ...

  5. C#中的静态成员和非静态成员

    C#的类中可以包含两种方法:C#静态方法与非静态方法.那么他们的定义有什么不同呢?他们在使用上会有什么不同呢? 让我们来看看最直观的差别:使用了static 修饰符的方法为静态方法,反之则是非静态方法 ...

  6. 根据自己的博客数据统计国内IT人群

    装上百度统计有一段时间了,今天突然找出报表看看,发现一个很有意思的事情.访问来源TOP5依次是:北京,上海,深圳,杭州,广州 虽然大部分文章都是当时特别白的时候记录下来的遇到过的问题,但受众确实应该只 ...

  7. http2.4简单配置

    前言: 上一篇博文说到了http的发展以及http完整请求响应的工作流程. 一.开篇: 从最简单的静态服务器开始. 之前说过,http是应用层协议,必定会在用户空间体现出具体的应用程序.常见的http ...

  8. C# 处理Word自动生成报告 一、概述

    经常遇到这样的需求, 生成Word格式的报告, 而不是单纯的一张表格的报表.  就像体检报告一样. 数据来源部分决定采用一个存储过程返回Dataset的方式, 整张报告的数据来源于此Dataset的多 ...

  9. Git命令行对照表

    git init # 初始化本地git仓库(创建新仓库) git config --global user.name "xxx" # 配置用户名 git config --glob ...

  10. js 事件冒泡和事件捕获

    事件流:指的是网页中元素接受事件的顺序,它是一个概念,而不是具体的实际的东西 事件冒泡:指的是内层元素的事件,会触发包含着此元素的外层元素的事件,触发的顺序是:由内而外的 例如: <!DOCTY ...