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

简介:

下面Himi来介绍第二个知识点:两种方式让精灵利用多帧播放动画

      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.     
  9. #import "cocos2d.h"   
  10.    
  11. @interface CCAnimation (Helper)   
  12. //直接索引图片名称   
  13. +(CCAnimation*) animationWithFile:(NSString*)name frameCount:(int)frameCount delay:(float)delay;   
  14. //利用帧缓存中的帧名称   
  15. +(CCAnimation*) animationWithFrame:(NSString*)frame frameCount:(int)frameCount delay:(float)delay;   
  16. @end   

 CCAnimationHelper.m

 

 
 
  1. //  CCAnimationHelper.m   
  2. //  SpriteProject   
  3. //   
  4. //  Created by Himi    
  5.    
  6. #import "CCAnimationHelper.h"   
  7.    
  8. @implementation CCAnimation (Helper)   
  9. //直接索引图片名称   
  10. +(CCAnimation*) animationWithFile:(NSString*)name frameCount:(int)frameCount delay:(float)delay   
  11. {   
  12.     NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];   
  13.     NSString* file;   
  14.     for (int i = 0; i < frameCount; i++)   
  15.     {    
  16.         file =nil;   
  17.         file = [NSString stringWithFormat:@"%@%i.png", name, i];   
  18.         CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];   
  19.         CGSize texSize = texture.contentSize;   
  20.         CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);   
  21.         CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect];   
  22.            
  23.         [frames addObject:frame];   
  24.     }    
  25.     return  [CCAnimation animationWithFrames:frames delay:delay];   
  26. }   
  27. //利用帧缓存中的帧名称   
  28. +(CCAnimation*) animationWithFrame:(NSString*)frame frameCount:(int)frameCount delay:(float)delay   
  29. {   
  30.     NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];   
  31.     NSString* file;   
  32.        
  33.     for (int i = 1; i <= frameCount; i++)   
  34.     {   
  35.         file =nil;   
  36.         file = [NSString stringWithFormat:@"%@%i.png", frame, i];   
  37.         CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];   
  38.         CCSpriteFrame* frame = [frameCache spriteFrameByName:file];   
  39.         [frames addObject:frame];   
  40.     }   
  41.     return  [CCAnimation animationWithFrames:frames delay:delay];   
  42. }   
  43. @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.    
  4. //利用文件名创建动作    
  5. //--首先导入#import "CCAnimationHelper.h"   
  6. MySprite*mySprite=[MySprite mySpriteInitWithImage:@"himi0.png"];   
  7. mySprite.position=ccp(140,mySprite.contentSize.height*0.5);   
  8. [self addChild:mySprite];   
  9.    
  10. CCAnimation*anim=[CCAnimation animationWithFile:@"himi" frameCount:12 delay:0.1];    
  11. CCAnimate* animate = [CCAnimate actionWithAnimation:anim];   
  12. CCSequence *seq = [CCSequence actions:animate,nil];    
  13. CCRepeatForever* repeat = [CCRepeatForever actionWithAction:seq];   
  14. [mySprite runAction:repeat];    
  15.    
  16.    
  17.     
  18. //利用帧缓存中的文件名创建动作    
  19. //--首先导入#import "CCAnimationHelper.h"   
  20. [[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"animationsFrames.plist"];   
  21. MySpriteByFrame *mySpriteByF =[MySpriteByFrame mySpriteInitWithFrameName:@"himi1.png"];   
  22. mySpriteByF.position=ccp(350,size.height*0.5);   
  23. [self addChild:mySpriteByF];   
  24.    
  25. anim=[CCAnimation animationWithFrame:@"himi" frameCount:12 delay:0.1];    
  26. animate = [CCAnimate actionWithAnimation:anim];   
  27. seq = [CCSequence actions:animate,nil];    
  28. repeat = [CCRepeatForever actionWithAction:seq];   
  29. [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.            
  10.         file = [NSString stringWithFormat:@"%@%i.png", frame, i];   
  11.         CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];   
  12.         CCSpriteFrame* frame = [frameCache spriteFrameByName:file];   
  13.         [frames addObject:frame];   
  14.     }   
  15.     return  [CCAnimation animationWithFrames:frames delay:delay];   
  16. }   

使用方法如下:

 

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

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

 

运行截图如下:

 

 

 

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

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

 

 










本文转自 xiaominghimi 51CTO博客,原文链接:http://blog.51cto.com/xiaominghimi/719899,如需转载请自行联系原作者
目录
相关文章
|
7月前
|
iOS开发
iOS 动画绘制圆形
iOS 动画绘制圆形
41 1
|
7月前
|
iOS开发
iOS多线程之NSOperationQueue-依赖、并发数、优先级、自定义Operation等最全的使用总结
iOS多线程之NSOperationQueue-依赖、并发数、优先级、自定义Operation等最全的使用总结
216 0
|
4月前
|
API 开发工具 iOS开发
在应用研发平台EMAS中,ios的推送有没有办法在app端设置在收到通知后是否展示的逻辑
在应用研发平台EMAS中,ios的推送有没有办法在app端设置在收到通知后是否展示的逻辑
32 1
|
12月前
|
iOS开发
iOS 15后设置导航控制器的导航条背景色无效的问题处理
iOS 15后设置导航控制器的导航条背景色无效的问题处理
368 0
|
7月前
|
API iOS开发
iOS 自定义转场动画 UIViewControllerTransitioning
iOS 自定义转场动画 UIViewControllerTransitioning
46 0
|
7月前
|
缓存 iOS开发
iOS LaunchScreen.storyboard 启动页设置图片不显示
iOS LaunchScreen.storyboard 启动页设置图片不显示
134 0
|
8月前
|
Swift iOS开发
iOS 13 之后自定义 Window 不显示解决 (SceneDelegate)
iOS 13 之后自定义 Window 不显示解决 (SceneDelegate)
256 0
|
10月前
|
小程序 API Android开发
小程序获取WIFI的API(IOS conncetWifi()自动跳转设置页)
小程序获取WIFI的API(IOS conncetWifi()自动跳转设置页)
257 0
|
Android开发 iOS开发 UED
iOS 自定义收款键盘
在iOS8之前,iOS系统的输入法只能使用苹果官方提供的输入法。 对于中文来说,官方的输入法并不好用,或者说不够好用,词库,联想,云输入等都没有或者和搜狗输入法,百度输入法等有中国特色的输入法相比有一定的差距。
167 0