【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧以及动画创建!【二】...
Himi这里就不细说了,直接提供给大家Himi封装好的两个方法:(Himi使用的cocos2d-iphone版本是1.0.0)
先唠叨一句,刚才上面说过了,创建精灵一种是利用直接索引文件名字来创建,另外一种就是直接利用帧缓存来创建,那么让一个精灵实现动画的播放当然也一样对应分为两种方式;直接上代码:
CCAnimationHelper.h
- //
- // CCAnimationHelper.h
- // SpriteProject
- //
- // Created by Himi on 11-8-6.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
- #import "cocos2d.h"
- @interface CCAnimation (Helper)
- //直接索引图片名称
- +(CCAnimation*) animationWithFile:(NSString*)name frameCount:(int)frameCount delay:(float)delay;
- //利用帧缓存中的帧名称
- +(CCAnimation*) animationWithFrame:(NSString*)frame frameCount:(int)frameCount delay:(float)delay;
- @end
CCAnimationHelper.m
- // CCAnimationHelper.m
- // SpriteProject
- //
- // Created by Himi
- #import "CCAnimationHelper.h"
- @implementation CCAnimation (Helper)
- //直接索引图片名称
- +(CCAnimation*) animationWithFile:(NSString*)name frameCount:(int)frameCount delay:(float)delay
- {
- NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];
- NSString* file;
- for (int i = 0; i < frameCount; i++)
- {
- file =nil;
- file = [NSString stringWithFormat:@"%@%i.png", name, i];
- CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];
- CGSize texSize = texture.contentSize;
- CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
- CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect];
- [frames addObject:frame];
- }
- return [CCAnimation animationWithFrames:frames delay:delay];
- }
- //利用帧缓存中的帧名称
- +(CCAnimation*) animationWithFrame:(NSString*)frame frameCount:(int)frameCount delay:(float)delay
- {
- NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];
- NSString* file;
- for (int i = 1; i <= frameCount; i++)
- {
- file =nil;
- file = [NSString stringWithFormat:@"%@%i.png", frame, i];
- CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
- CCSpriteFrame* frame = [frameCache spriteFrameByName:file];
- [frames addObject:frame];
- }
- return [CCAnimation animationWithFrames:frames delay:delay];
- }
- @end
- +(CCAnimation*) animationWithFile:(NSString*)name frameCount:(int)frameCount delay:(float)delay{};
- //参数讲解:name:资源文件名 ;frameCount 总帧数 ; delay :每一帧的刷新时间
- +(CCAnimation*) animationWithFrame:(NSString*)frame frameCount:(int)frameCount delay:(float)delay{};
- //参数讲解:frame:帧文件名 ;frameCount 总帧数 ; delay :每一帧的刷新时间
注意:1、 类有(help)的表示对原有的类进行扩展;2、动作帧都要按照himi0.png,himi1.png,himi2.png,这样子命名,当然拉你不想这样可以修改这两个方法即可;
3. 注意Himi这里的两个方法,一个是从0开始喔,另外一个是从1开始的,如果你用帧缓存进行创建动作就要从himi1.png,开始命名,嘿嘿~
下面是使用方法:
- //--@@@@@@@--第二个知识点--@@@@@@@
- //利用文件名创建动作
- //--首先导入#import "CCAnimationHelper.h"
- MySprite*mySprite=[MySprite mySpriteInitWithImage:@"himi0.png"];
- mySprite.position=ccp(140,mySprite.contentSize.height*0.5);
- [self addChild:mySprite];
- CCAnimation*anim=[CCAnimation animationWithFile:@"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];
- //利用帧缓存中的文件名创建动作
- //--首先导入#import "CCAnimationHelper.h"
- [[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"animationsFrames.plist"];
- MySpriteByFrame *mySpriteByF =[MySpriteByFrame mySpriteInitWithFrameName:@"himi1.png"];
- mySpriteByF.position=ccp(350,size.height*0.5);
- [self addChild:mySpriteByF];
- anim=[CCAnimation animationWithFrame:@"himi" frameCount:12 delay:0.1];
- animate = [CCAnimate actionWithAnimation:anim];
- seq = [CCSequence actions:animate,nil];
- repeat = [CCRepeatForever actionWithAction:seq];
- [mySpriteByF runAction:repeat];
这里要提醒童鞋们的有两点:
1.利用帧缓存创建动画的时候要注意要提前将帧加载到缓存里喔~
2.Himi这两个方法没有写一样,所以动作帧的命名一个从0开始,另外一个从1开始!童鞋们可以自行改过来哈
运行截图如下:
第三点知识点:为你的精灵设置攻击帧;
首先跟一些童鞋简单说下何谓攻击帧,假如主角攻击一个怪物的时候,肯定播放攻击动作,但是!你是在攻击动作开始的时候就扣怪物血还是攻击动作结束后扣怪物血呢?都不是!!!因为很不真实!所以我们应该当攻击动作播放到设定的某一帧的时候进行扣怪物血或者其他逻辑,然后继续播放剩下的攻击动作,这样才更加的真实!
那么OK,这里Himi仍然封装成一个方法让你直接使用即可;首先看下代码:
- <strong>//带有攻击帧的动画
- </strong>+(CCAnimation*) animationWithFrameFromStartFrameIndex:(NSString*)frame startFrameCountIndex:(int)startFrameIndex frameCount:(int)frameCount delay:(float)delay
- {
- NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];
- NSString* file;
- file =nil;
- for (int i = startFrameIndex; i < frameCount+startFrameIndex; i++)
- {
- file = [NSString stringWithFormat:@"%@%i.png", frame, i];
- CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
- CCSpriteFrame* frame = [frameCache spriteFrameByName:file];
- [frames addObject:frame];
- }
- return [CCAnimation animationWithFrames:frames delay:delay];
- }
使用方法如下:
- <strong> </strong> //--@@@@@@@--第三个知识点--@@@@@@@
- [[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"animationsFrames.plist"];
- MySpriteByFrame *mySpriteAni =[MySpriteByFrame mySpriteInitWithFrameName:@"himi1.png"];
- mySpriteAni.position=ccp(260,size.height*0.5);
- [self addChild:mySpriteAni];
- //首先执行前6帧动画
- CCAnimation*anim=[CCAnimation animationWithFrameFromStartFrameIndex:@"himi" startFrameCountIndex:1 frameCount:6 delay:0.1];
- CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
- //攻击帧执行的函数
- CCCallFunc *downEnemyHp =[CCCallFunc actionWithTarget:self selector:@selector(downEnemyHp)];
- //后6帧动画
- anim=[CCAnimation animationWithFrameFromStartFrameIndex:@"himi" startFrameCountIndex:7 frameCount:6 delay:0.1 ];
- CCAnimate* animateForAttackIndex = [CCAnimate actionWithAnimation:anim];
- CCSequence *seq = [CCSequence actions:animate,downEnemyHp,animateForAttackIndex,nil];
- [mySpriteAni runAction:seq];
- ---------回调函数
- -(void)downEnemyHp{
- CCLabelTTF *label = (CCLabelTTF*)[self getChildByTag:99];
- [label setString:@"攻击帧"];
- }
前六帧-》回调downEnemyHp函数-》继续播放剩下的播放帧数
运行截图如下:
OK,继续忙了~由于本文知识点较多和较细节,这里Himi放出源码,我的动作相关的封装都在CCAnimationHelper.h/.m中喔,注意不要改类名,因为这个类是Himi对cocos2d源码进行的扩展!
源码地址: http://www.himigame.com/iphone-cocos2d/525.html
【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧以及动画创建!【二】...相关推荐
- 【cocos2d游戏开发实战】一款射击类小游戏《Zombie Age》的开发(一)
前言 因为学习的原因需要完成一款射击类的小游戏,之前做的飞机大战没有达到要求,在网上找了几款射击类游戏后因为素材和难度的关系最后决定山寨一下<僵尸时代2>这款小游戏,开发到游戏主体基本完整 ...
- 【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧(指定开始帧)以及扩展Cocos2d源码的CCAnimation简化动画创建!...
原文链接: http://www.himigame.com/iphone-cocos2d/525.html 本篇的知识点如下: 1. 两种方式实现自定义精灵: 2.两种方 ...
- 初学者开发iOS游戏的入门必备用书——iOS 5 cocos2d游戏开发实战(第2版)
如果想接触iOS游戏开发,今天笔者就推荐一本"大杀器"--<iOS 5 cocos2d游戏开发实战(第2版)>(当当链接) 为零基础的"设想过自己写个电脑游戏 ...
- iOS 5 cocos2d游戏开发实战(第2版)
<iOS 5 cocos2d游戏开发实战(第2版)> 基本信息 作者: [美]Steffen Itterheim Andreas Low [作译者介绍] 译者: 同济大学苹果俱乐部 丛书名 ...
- Cocos2d游戏开发之如何解包获得pvr.ccz中的美术资源
Cocos2d游戏开发之如何解包获得pvr.ccz中的美术资源 在大部分时候,Cocos2d游戏会将美术资源打包以减轻性能要求,提高加载速度.而如果我们需要获得其中的美术资源,就得解包.这里以plis ...
- [转]cocos2d游戏开发,常用工具集合
cocos2d游戏开发,常用工具集合 原文地址:http://www.cocoachina.com/bbs/read.php?tid-68951.html 位图字体工具Bitmap Font Tool ...
- 《C++游戏开发》笔记十一 平滑动画:不再颤抖的小雪花
本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9430645 作者:七十一雾央 新浪微博:http:// ...
- 【iOS-Cocos2d游戏开发之二】Cocos2D 游戏开发资源贴(教程以及源码)
李华明Himi 原创,转载务必在明显处注明: 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2d/420.html 这两天抽出 ...
- 【Visual C++】游戏开发笔记四十一 浅墨DirectX教程之九 为三维世界添彩:纹理映射技术(一)...
本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接: http://blog.csdn.net/zhmxy555/article/details/8523341 作者:毛星云(浅墨 ...
最新文章
- 网络协议OSI、TCP/IP协议、Socket套接字和第三方AsyncSock的使用等解析
- Unity应用架构设计(7)——IoC工厂理念先行
- ASP.NET 设计模式中依赖倒置原则
- 深入浅出 ASP.NET Core 与 Docker 入门课程说明
- java web主流框架整合开发 光盘_开发者突击:Java Web主流框架整合开发(第2版)(附CD光盘1张)...
- C语言之typedef定义函数指针(十三)
- android ImageView加圆角
- CAM350对比电路图方法
- 重定向和请求转发详解
- Windows Message Queue 优先队列
- 阿里云服务器远程桌面安装
- 长沙python培训_长的解释|长的意思|汉典“长”字的基本解释
- 中国人的宴席座次礼仪
- C++中一些可以在偷懒时直接使用的函数
- 从事互联网产品运营所需的8条技能
- node.js毕业设计安卓电子阅读器APP(程序+APP+LW)
- 如何把webp压缩大小?在线webp压缩的工具推荐
- 2017年12月英语六级翻译洞庭湖
- 储能辅助电力系统调峰的容量需求优化配置matlab/cplex
- 天池比赛短租数据集分析之数据图表
热门文章
- mybatis实战总结
- 设计模式系列之「装饰模式」
- Java使用RSA加密解密及签名校验
- ansible 使用主机IP批量修改机器名
- asa802.k8-telnet for lan-base
- centos 8 kubernetes安装详解_电力工程必学!8方面施工图文详解,电气安装工程施工工艺...
- Android 几种解析XML文件方式的区别
- Android OnLowMemory和OnTrimMemory
- mybatis分页应用
- Android sqlite数据库操作通用框架AHibernate(一)-CRUD示例和使用步骤