`
yangping_Account
  • 浏览: 187121 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

Cocos2D自定义精灵类并为你的精灵设置攻击帧

 
阅读更多

 

 上周貌似没有写新的博文,那么今天Himi写个精品的博文奉献给童鞋们;

(不少童鞋说Himi的教程最近都没有源码放出=。 =,这里我解释下,一般我没有放出源码的博文那肯定已经将代码贴出来了,这点是肯定的,否则Himi一定给出源码的)

       本篇的知识点如下:

       1. 两种方式实现自定义精灵;

       2.两种方式让精灵利用多帧播放动画

       3. 为你的精灵设置带有攻击帧的动画,当执行攻击动作的中间会执行扣血等逻辑,然后接着播放动作喔~

       首先第一种如何自定义精灵:

       两种自定义一个精灵当然无疑我们仍然继承CCSprite,首先看第一种自定义方式,Himi新建一个类,名字是MySprite,代码如下,大家一看就很清晰了;

          MySprite.h



//  

//  MySprite.h  

//  HimiAnimationsTestPro  

//  

//  Created by 华明 李 on 11-11-20.  

//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.  

//  

  

#import "CCSprite.h"  

  

@interface MySprite : CCSprite{  

      

}  

+(id) mySpriteInitWithImage:(NSString*)fileName;  

-(id) initWithMySpriteImage:(NSString*)fileName;  

@end  



         MySprite.m



//  

//  MySprite.m  

//  HimiAnimationsTestPro  

//  

//  Created by 华明 李 on 11-11-20.  

//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.  

//  

  

#import "MySprite.h"  

  

@implementation MySprite  

+(id) mySpriteInitWithImage:(NSString*)fileName  

{  

    return [[[self alloc] initWithMySpriteImage:fileName] autorelease];//这里仿照cocos2d原理,自动清理精灵  

}  

  

-(id) initWithMySpriteImage:(NSString*)fileName  

{  

    if ((self = [super initWithFile:fileName]))  

    {  

        //初始化的东东都写在这里喔~  

    }  

    return self;  

}    

  

-(void) dealloc  

{  

   //内存清理  

    [super dealloc];  

}  

@end  


大家以后自定义精灵的时候可以将我这个当模版即可!如果你不想自定义的精灵传参,那就直接自己修改下构造函数即可,初始化的时候写死名字即可(比如一般游戏主角不需要传入图片名字作为参数,直接在我们主角类的构造中将图片资源名写死即可) 


    然后我们用第二种方式,所谓第二种方式其实就是修改我们的初始化函数,让其精灵初始化的方式改成帧缓存创建:(适合利用TP打包工具出的图进行来创建精灵)

代码如下:(这里Himi为了让童鞋们看得清楚,Himi新建一个类,名字是MySpriteByFrame)


         MySpriteByFrame.h


//  

//  MySprite.h  

//  HimiAnimationsTestPro  

//  

//  Created by 华明 李 on 11-11-20.  

//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.  

//  

  

#import "CCSprite.h"  

  

@interface MySpriteByFrame : CCSprite{  

      

}  

+(id) mySpriteInitWithFrameName:(NSString*)frameName;  

-(id) initWithMySpriteFrameName:(NSString*)frameName;  

@end  



           MySpriteByFrame.m



//  

//  MySprite.m  

//  HimiAnimationsTestPro  

//  

//  Created by 华明 李 on 11-11-20.  

//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.  

//  

  

#import "MySpriteByFrame.h"  

  

@implementation MySpriteByFrame  

 

 

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

                   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开始!童鞋们可以自行改过来哈

运行截图如下:

          

【扯皮一下,如果你在我的Android或者iOS群中,你感觉这张哆啦A梦图熟悉不~嘿嘿,Himi的7个群都是这个GIF做为群头像,娃哈哈,我自己做的 娃哈哈;】

 

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

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

   那么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];  

}  



+(CCAnimation*) animationWithFrameFromStartFrameIndex:(NSString*)frame startFrameCountIndex:(int)startFrameIndex frameCount:(int)frameCount delay:(float)delay  {}  

//参数介绍:frame :帧名字;     startFrameIndex:指定播放起始帧 ;   frameCount:帧总数   ; delay:每帧的刷新时间  


使用方法如下:


      //--@@@@@@@--第三个知识点--@@@@@@@  

          

        [[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源码进行的扩展

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics