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