cocos2d中CCAnimation的使用(cocos2d 1.0以上版本)
CCAnimation animation = CCAnimation.animationWithFrames(list);
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"animationsFrames.plist"];//左侧正常速度的播放 CCSprite*mySprite=[CCSprite spriteWithSpriteFrameName:@"himi1.png"]; mySprite.position=ccp(120,150); [self addChild:mySprite]; CCAnimation*anim=[CCAnimation animationWithFrame:@"himi" frameCount:12 delay:0.1]; CCAnimate* animate = [CCAnimate actionWithAnimation:anim]; CCSequence *seq = [CCSequence actions:animate,nil]; CCRepeatForever* repeat = [CCRepeatForever actionWithAction:seq]; [mySprite runAction:repeat];
而animationWithFrames的参数是一个NSArray类型的数据,所以要经过一些转换后才能实现,具体实现参照如下例子:
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"***.png"];
CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:texture rect:CGRectMake(0, 0, texture.contentSize.width, texture.contentSize.height)];
NSArray *array = [[NSArray alloc] initWithObjects:frame, nil];
CCAnimation *animation = [CCAnimation animationWithFrames:array delay:0.2f];
for(int i = 0; i < 3; i++)
{
int x = i % 3;
[animation addFrameWithTexture:mgr.texture rect:CGRectMake(x*32, 0, 31, 30)];
}
id action = [CCAnimate actionWithAnimation:animation];
[flight runAction:[CCRepeatForever actionWithAction:action]];
此处的fight是CCSprite类型的变量
附:c++风格代码
CCTexture2D texture = CCTextureCache.sharedTextureCache().addImage(@"Animation\AndyBogard_by_SWP"); CCSpriteFrame fame0 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 0, 48 * 0, 32, 48)); CCSpriteFrame fame1 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 1, 48 * 0, 32, 48)); CCSpriteFrame fame2 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 2, 48 * 0, 32, 48)); CCSpriteFrame fame3 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 3, 48 * 0, 32, 48)); List<CCSpriteFrame> list = new List<CCSpriteFrame>(); list.Add(fame0); list.Add(fame1); list.Add(fame2); list.Add(fame3); CCAnimation animation = CCAnimation.animationWithFrames(list); CCSprite sprite = CCSprite.spriteWithSpriteFrame(fame0); sprite.position = new CCPoint(200, 200); addChild(sprite); CCAnimate animate = CCAnimate.actionWithAnimation(animation); animate.duration = 0.5f; sprite.runAction(CCRepeatForever.actionWithAction(animate));
cocos2d-x for xna 学习笔记01:基本动画实现
基于cocos2d-x的动画都是采用了Action的方式来实现,这样做的导致了实现一个简单的动画就要写比较多的代码。在实际项目和开发中,可能需要我们自己去封装。
下面介绍一下,怎么在cocos2d-x for xna 中实现一个基本的动画。我准备一张动画源图,如下:
新建一个helloworld项目后,把图片加入到项目资源中。
在init()函数加入以下代码
#region 简单动画测试 CCTexture2D texture = CCTextureCache.sharedTextureCache().addImage(@"Animation\AndyBogard_by_SWP"); CCSpriteFrame fame0 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 0, 48 * 0, 32, 48)); CCSpriteFrame fame1 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 1, 48 * 0, 32, 48)); CCSpriteFrame fame2 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 2, 48 * 0, 32, 48)); CCSpriteFrame fame3 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 3, 48 * 0, 32, 48)); List<CCSpriteFrame> list = new List<CCSpriteFrame>(); list.Add(fame0); list.Add(fame1); list.Add(fame2); list.Add(fame3); CCAnimation animation = CCAnimation.animationWithFrames(list); CCSprite sprite = CCSprite.spriteWithSpriteFrame(fame0); sprite.position = new CCPoint(200, 200); addChild(sprite); CCAnimate animate = CCAnimate.actionWithAnimation(animation); animate.duration = 0.5f; sprite.runAction(CCRepeatForever.actionWithAction(animate)); #endregion
编辑运行就可以见到效果了。
简单过程是,使用CCTexture2D加载图片 ,用CCTexture2D生成对应的CCSpriteFrame(对应的就是帧),将CCSpriteFrame添加到CCAnimation生成动画数据,用CCAnimation生成CCAnimate(就是最终的动画动作),最后用CCSprite执行这个动作。
cocos2d中CCAnimation的使用(cocos2d 1.0以上版本)相关推荐
- 深入解读 Knative Eventing 0.7 版本新特性
前言 Knative Eventing 0.7 版本已经于 6 月 26 号正式发布.本次发布主要围绕重构 Channel 特性展开.本篇文章重点解读了这些特性,并且以此展望一下 Knative Ev ...
- Cocos2d中使用颜色混合:加算,减算
Cocos2d中使用颜色混合:加算,减算 转自http://blog.sina.com.cn/s/blog_7a2ffd5c0100xtid.html CCSprite有一个ccBlendFunc类型 ...
- 疯狂ios之cocos2d中的声音
13.13 cocos2d中的声音 任何一个游戏中都不能缺少音乐和音效,苹果公司在iOS系统中提供了两个框架用于播放音乐,分别是AVAudioPlayer和OpenAL.使用AVAudioPlayer ...
- 在Cocos2d中实现能够惯性拖动的选择界面
苹果的应用讲究用户体验 有的时候仔细想想 的确,很多细节决定了用户体验 比如说惯性拖动 可以说之前没有任何一家厂商能把触摸惯性拖动做的像苹果的UI那么流畅 Cocos2D中实现能够惯性拖动的选择界面 ...
- cocos2d中的Color3B、Color4B、Color4F的使用
首先给出cocos2d标准库中的三个类的构造函数和定义,他们也是可以互相转换的 cocos2d中表示颜色有三种对象: Color3B 用三个 0-255 的整数描述颜色. Color4B 用四个 ...
- cocos2d 中判断CGPoint或者CGSize是否相等
cocos2d 中判断CGPoint是否相等 调用CGPointEqualToPoint(point1, point2) 判断CGSize是否相等 调用CGSizeEqualToSize(size1, ...
- 疯狂ios之cocos2d中的文本
在游戏当中经常需要添加标签和文本对此cocos2d提供了强大的文本渲染功能.cocos2d支持所有内置的iOS字体以及一些TrueType字体. 在cocos2d中文本渲染功能通常由两个类实现CCLa ...
- Cocos2D中图片加-hd后缀的说明
你可能注意到实际上游戏中的sprite都有2张图片,它都对应该精灵,并包含在资源包中(resource pack): player.png(27x40 pixels)和player-hd.png(do ...
- cocos2d中,设置层的可视区域
http://www.cocoachina.com/bbs/read.php?tid=97164 cocos2d中,设置层的可视区域在真机上不管用 -(void) visit{ glEnabl ...
最新文章
- xilinx FIFO
- go gdb调试 参数设置 减小执行文件体积
- java中可以用浮点作为循环变量吗_Java千问:Java循环语句的几个冷门知识点你都知道吗?...
- [转载] Spring面试题整理
- 1-4flink概述
- Angular4.x+Ionic3 踩坑之路之打包时出现JAVASCRIPT HEAP OUT OF MEMORY的几种解决办法
- [转载] 晓说——第23期:大师照亮八十年代
- python随机数据库_Python实现生成随机数据插入mysql数据库的方法
- 基于分数阶傅里叶变换的chirp信号检测与参数估计(原理附代码)
- 智能人物画像综合分析系统——Day3
- 台式计算机调整显示亮度,台式电脑显示器屏幕亮度怎么调节?
- 保存图片到相册并打开微信扫一扫
- linux网络打印机安装步骤,科学网—CentOS6.5上使用cups安装网络打印机 - 王敏玲的博文...
- 介绍两种Revit绘制斜墙的方法及快速【梁随斜板】
- 【数据结构与算法】之深入解析“航班预订统计”的求解思路与算法示例
- bitcode 是什么_dictate什么意思
- Python的10086查询系统模拟
- dreamware html中加入flv,html中怎样嵌入flv格式文件
- 数码产品交易微信小程序的设计与实现
- 通过三种情况深度分析,复杂的公网环境,网络穿透如何做到?丨C++后端开发丨P2P丨c/c++Linux服务器开发丨网关API
热门文章
- php tp3.0计算每天的订单,TP5.1结合taskphp3.0定时任务
- 分区表自动维护 mysql_Oracle 10g分区表的自动维护
- 色环电感外部磁场泄漏
- 分析无线充电线圈产生的导航信号在自绕工字型电感中的感应电动势
- CSDN上究竟可以上载多大的GIF文件?
- onkeypress属性添加报错_亚马逊运营过程中listing常见报错及应对策略汇总
- linux 下orapwd 未找到命令,orapwd命令中entries参数的作用
- 显示计算机硬盘驱动器更改,笔记本硬盘驱动器的字母怎么修改?笔记本修改硬盘驱动器字母的方法...
- 格式工厂mac版_格式工厂无广告版,支持PDF文件的转换
- 对未标记为可安全执行的脚本_Script Debugger for Mac(脚本调试软件)