Himi这里就不细说了,直接提供给大家Himi封装好的两个方法:(Himi使用的cocos2d-iphone版本是1.0.0)

   先唠叨一句,刚才上面说过了,创建精灵一种是利用直接索引文件名字来创建,另外一种就是直接利用帧缓存来创建,那么让一个精灵实现动画的播放当然也一样对应分为两种方式;直接上代码:

CCAnimationHelper.h

  1. //
  2. //  CCAnimationHelper.h
  3. //  SpriteProject
  4. //
  5. //  Created by Himi on 11-8-6.
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "cocos2d.h"
  9. @interface CCAnimation (Helper)
  10. //直接索引图片名称
  11. +(CCAnimation*) animationWithFile:(NSString*)name frameCount:(int)frameCount delay:(float)delay;
  12. //利用帧缓存中的帧名称
  13. +(CCAnimation*) animationWithFrame:(NSString*)frame frameCount:(int)frameCount delay:(float)delay;
  14. @end

 CCAnimationHelper.m

  1. //  CCAnimationHelper.m
  2. //  SpriteProject
  3. //
  4. //  Created by Himi
  5. #import "CCAnimationHelper.h"
  6. @implementation CCAnimation (Helper)
  7. //直接索引图片名称
  8. +(CCAnimation*) animationWithFile:(NSString*)name frameCount:(int)frameCount delay:(float)delay
  9. {
  10. NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];
  11. NSString* file;
  12. for (int i = 0; i < frameCount; i++)
  13. {
  14. file =nil;
  15. file = [NSString stringWithFormat:@"%@%i.png", name, i];
  16. CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];
  17. CGSize texSize = texture.contentSize;
  18. CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
  19. CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect];
  20. [frames addObject:frame];
  21. }
  22. return  [CCAnimation animationWithFrames:frames delay:delay];
  23. }
  24. //利用帧缓存中的帧名称
  25. +(CCAnimation*) animationWithFrame:(NSString*)frame frameCount:(int)frameCount delay:(float)delay
  26. {
  27. NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];
  28. NSString* file;
  29. for (int i = 1; i <= frameCount; i++)
  30. {
  31. file =nil;
  32. file = [NSString stringWithFormat:@"%@%i.png", frame, i];
  33. CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
  34. CCSpriteFrame* frame = [frameCache spriteFrameByName:file];
  35. [frames addObject:frame];
  36. }
  37. return  [CCAnimation animationWithFrames:frames delay:delay];
  38. }
  39. @end
  1. +(CCAnimation*) animationWithFile:(NSString*)name frameCount:(int)frameCount delay:(float)delay{};
  2. //参数讲解:name:资源文件名  ;frameCount 总帧数   ;   delay :每一帧的刷新时间
  3. +(CCAnimation*) animationWithFrame:(NSString*)frame frameCount:(int)frameCount delay:(float)delay{};
  4. //参数讲解:frame:帧文件名  ;frameCount 总帧数   ;   delay :每一帧的刷新时间

注意:1、 类有(help)的表示对原有的类进行扩展;2、动作帧都要按照himi0.png,himi1.png,himi2.png,这样子命名,当然拉你不想这样可以修改这两个方法即可;

            3. 注意Himi这里的两个方法,一个是从0开始喔,另外一个是从1开始的,如果你用帧缓存进行创建动作就要从himi1.png,开始命名,嘿嘿~


下面是使用方法:

  1. //--@@@@@@@--第二个知识点--@@@@@@@
  2. //利用文件名创建动作
  3. //--首先导入#import "CCAnimationHelper.h"
  4. MySprite*mySprite=[MySprite mySpriteInitWithImage:@"himi0.png"];
  5. mySprite.position=ccp(140,mySprite.contentSize.height*0.5);
  6. [self addChild:mySprite];
  7. CCAnimation*anim=[CCAnimation animationWithFile:@"himi" frameCount:12 delay:0.1];
  8. CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
  9. CCSequence *seq = [CCSequence actions:animate,nil];
  10. CCRepeatForever* repeat = [CCRepeatForever actionWithAction:seq];
  11. [mySprite runAction:repeat];
  12. //利用帧缓存中的文件名创建动作
  13. //--首先导入#import "CCAnimationHelper.h"
  14. [[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"animationsFrames.plist"];
  15. MySpriteByFrame *mySpriteByF =[MySpriteByFrame mySpriteInitWithFrameName:@"himi1.png"];
  16. mySpriteByF.position=ccp(350,size.height*0.5);
  17. [self addChild:mySpriteByF];
  18. anim=[CCAnimation animationWithFrame:@"himi" frameCount:12 delay:0.1];
  19. animate = [CCAnimate actionWithAnimation:anim];
  20. seq = [CCSequence actions:animate,nil];
  21. repeat = [CCRepeatForever actionWithAction:seq];
  22. [mySpriteByF runAction:repeat];

这里要提醒童鞋们的有两点:

      1.利用帧缓存创建动画的时候要注意要提前将帧加载到缓存里喔~

      2.Himi这两个方法没有写一样,所以动作帧的命名一个从0开始,另外一个从1开始!童鞋们可以自行改过来哈

运行截图如下:

第三点知识点:为你的精灵设置攻击帧;

    首先跟一些童鞋简单说下何谓攻击帧,假如主角攻击一个怪物的时候,肯定播放攻击动作,但是!你是在攻击动作开始的时候就扣怪物血还是攻击动作结束后扣怪物血呢?都不是!!!因为很不真实!所以我们应该当攻击动作播放到设定的某一帧的时候进行扣怪物血或者其他逻辑,然后继续播放剩下的攻击动作,这样才更加的真实!

   那么OK,这里Himi仍然封装成一个方法让你直接使用即可;首先看下代码:

  1. <strong>//带有攻击帧的动画
  2. </strong>+(CCAnimation*) animationWithFrameFromStartFrameIndex:(NSString*)frame startFrameCountIndex:(int)startFrameIndex frameCount:(int)frameCount delay:(float)delay
  3. {
  4. NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];
  5. NSString* file;
  6. file =nil;
  7. for (int i = startFrameIndex; i < frameCount+startFrameIndex; i++)
  8. {
  9. file = [NSString stringWithFormat:@"%@%i.png", frame, i];
  10. CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
  11. CCSpriteFrame* frame = [frameCache spriteFrameByName:file];
  12. [frames addObject:frame];
  13. }
  14. return  [CCAnimation animationWithFrames:frames delay:delay];
  15. }

使用方法如下:

  1. <strong> </strong>       //--@@@@@@@--第三个知识点--@@@@@@@
  2. [[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"animationsFrames.plist"];
  3. MySpriteByFrame *mySpriteAni =[MySpriteByFrame mySpriteInitWithFrameName:@"himi1.png"];
  4. mySpriteAni.position=ccp(260,size.height*0.5);
  5. [self addChild:mySpriteAni];
  6. //首先执行前6帧动画
  7. CCAnimation*anim=[CCAnimation animationWithFrameFromStartFrameIndex:@"himi" startFrameCountIndex:1 frameCount:6 delay:0.1];
  8. CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
  9. //攻击帧执行的函数
  10. CCCallFunc *downEnemyHp =[CCCallFunc actionWithTarget:self selector:@selector(downEnemyHp)];
  11. //后6帧动画
  12. anim=[CCAnimation animationWithFrameFromStartFrameIndex:@"himi" startFrameCountIndex:7 frameCount:6 delay:0.1 ];
  13. CCAnimate* animateForAttackIndex = [CCAnimate actionWithAnimation:anim];
  14. CCSequence *seq = [CCSequence actions:animate,downEnemyHp,animateForAttackIndex,nil];
  15. [mySpriteAni runAction:seq];
  16. ---------回调函数
  17. -(void)downEnemyHp{
  18. CCLabelTTF *label = (CCLabelTTF*)[self getChildByTag:99];
  19. [label setString:@"攻击帧"];
  20. }

前六帧-》回调downEnemyHp函数-》继续播放剩下的播放帧数

运行截图如下:

  OK,继续忙了~由于本文知识点较多和较细节,这里Himi放出源码,我的动作相关的封装都在CCAnimationHelper.h/.m中喔,注意不要改类名,因为这个类是Himi对cocos2d源码进行的扩展!

     源码地址:  http://www.himigame.com/iphone-cocos2d/525.html

本文转自 xiaominghimi 51CTO博客,原文链接:http://blog.51cto.com/xiaominghimi/719899,如需转载请自行联系原作者

【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧以及动画创建!【二】...相关推荐

  1. 【cocos2d游戏开发实战】一款射击类小游戏《Zombie Age》的开发(一)

    前言 因为学习的原因需要完成一款射击类的小游戏,之前做的飞机大战没有达到要求,在网上找了几款射击类游戏后因为素材和难度的关系最后决定山寨一下<僵尸时代2>这款小游戏,开发到游戏主体基本完整 ...

  2. 【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧(指定开始帧)以及扩展Cocos2d源码的CCAnimation简化动画创建!...

    原文链接: http://www.himigame.com/iphone-cocos2d/525.html  本篇的知识点如下:        1. 两种方式实现自定义精灵:        2.两种方 ...

  3. 初学者开发iOS游戏的入门必备用书——iOS 5 cocos2d游戏开发实战(第2版)

    如果想接触iOS游戏开发,今天笔者就推荐一本"大杀器"--<iOS 5 cocos2d游戏开发实战(第2版)>(当当链接) 为零基础的"设想过自己写个电脑游戏 ...

  4. iOS 5 cocos2d游戏开发实战(第2版)

    <iOS 5 cocos2d游戏开发实战(第2版)> 基本信息 作者: [美]Steffen Itterheim Andreas Low [作译者介绍] 译者: 同济大学苹果俱乐部 丛书名 ...

  5. Cocos2d游戏开发之如何解包获得pvr.ccz中的美术资源

    Cocos2d游戏开发之如何解包获得pvr.ccz中的美术资源 在大部分时候,Cocos2d游戏会将美术资源打包以减轻性能要求,提高加载速度.而如果我们需要获得其中的美术资源,就得解包.这里以plis ...

  6. [转]cocos2d游戏开发,常用工具集合

    cocos2d游戏开发,常用工具集合 原文地址:http://www.cocoachina.com/bbs/read.php?tid-68951.html 位图字体工具Bitmap Font Tool ...

  7. 《C++游戏开发》笔记十一 平滑动画:不再颤抖的小雪花

    本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9430645 作者:七十一雾央 新浪微博:http:// ...

  8. 【iOS-Cocos2d游戏开发之二】Cocos2D 游戏开发资源贴(教程以及源码)

     李华明Himi 原创,转载务必在明显处注明: 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2d/420.html 这两天抽出 ...

  9. 【Visual C++】游戏开发笔记四十一 浅墨DirectX教程之九 为三维世界添彩:纹理映射技术(一)...

    本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接: http://blog.csdn.net/zhmxy555/article/details/8523341 作者:毛星云(浅墨 ...

最新文章

  1. 网络协议OSI、TCP/IP协议、Socket套接字和第三方AsyncSock的使用等解析
  2. Unity应用架构设计(7)——IoC工厂理念先行
  3. ASP.NET 设计模式中依赖倒置原则
  4. 深入浅出 ASP.NET Core 与 Docker 入门课程说明
  5. java web主流框架整合开发 光盘_开发者突击:Java Web主流框架整合开发(第2版)(附CD光盘1张)...
  6. C语言之typedef定义函数指针(十三)
  7. android ImageView加圆角
  8. CAM350对比电路图方法
  9. 重定向和请求转发详解
  10. Windows Message Queue 优先队列
  11. 阿里云服务器远程桌面安装
  12. 长沙python培训_长的解释|长的意思|汉典“长”字的基本解释
  13. 中国人的宴席座次礼仪
  14. C++中一些可以在偷懒时直接使用的函数
  15. 从事互联网产品运营所需的8条技能
  16. node.js毕业设计安卓电子阅读器APP(程序+APP+LW)
  17. 如何把webp压缩大小?在线webp压缩的工具推荐
  18. 2017年12月英语六级翻译洞庭湖
  19. 储能辅助电力系统调峰的容量需求优化配置matlab/cplex
  20. 天池比赛短租数据集分析之数据图表

热门文章

  1. mybatis实战总结
  2. 设计模式系列之「装饰模式」
  3. Java使用RSA加密解密及签名校验
  4. ansible 使用主机IP批量修改机器名
  5. asa802.k8-telnet for lan-base
  6. centos 8 kubernetes安装详解_电力工程必学!8方面施工图文详解,电气安装工程施工工艺...
  7. Android 几种解析XML文件方式的区别
  8. Android OnLowMemory和OnTrimMemory
  9. mybatis分页应用
  10. Android sqlite数据库操作通用框架AHibernate(一)-CRUD示例和使用步骤