下面Himi來介紹第二個知識點:兩種方式讓精靈利用多幀播放動畫
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
// 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
for (int i = 1; i <= frameCount; i++)
{
file = [NSString stringWithFormat:@"%@%i.png", frame, i];
CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCSpriteFrame* frame = [frameCache spriteFrameByName:file];
}
+(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];
//利用幀緩存中的檔案名建立動作
[[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開始!童鞋們可以自行改過來哈
運作截圖如下:
<a target="_blank" href="http://blog.51cto.com/attachment/201111/233952667.png"></a>
第三點知識點:為你的精靈設定攻擊幀;
首先跟一些童鞋簡單說下何謂攻擊幀,假如主角攻擊一個怪物的時候,肯定播放攻擊動作,但是!你是在攻擊動作開始的時候就扣怪物血還是攻擊動作結束後扣怪物血呢?都不是!!!因為很不真實!是以我們應該當攻擊動作播放到設定的某一幀的時候進行扣怪物血或者其他邏輯,然後繼續播放剩下的攻擊動作,這樣才更加的真實!
那麼OK,這裡Himi仍然封裝成一個方法讓你直接使用即可;首先看下代碼:
<strong>//帶有攻擊幀的動畫
</strong>+(CCAnimation*) animationWithFrameFromStartFrameIndex:(NSString*)frame startFrameCountIndex:(int)startFrameIndex frameCount:(int)frameCount delay:(float)delay
file =nil;
for (int i = startFrameIndex; i < frameCount+startFrameIndex; i++)
使用方法如下:
<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函數-》繼續播放剩下的播放幀數
<a target="_blank" href="http://blog.51cto.com/attachment/201111/234037782.png"></a>
<a target="_blank" href="http://blog.51cto.com/attachment/201111/234048464.png"></a>
OK,繼續忙了~由于本文知識點較多和較細節,這裡Himi放出源碼,我的動作相關的封裝都在CCAnimationHelper.h/.m中喔,注意不要改類名,因為這個類是Himi對cocos2d源碼進行的擴充!
本文轉自 xiaominghimi 51CTO部落格,原文連結:http://blog.51cto.com/xiaominghimi/719899,如需轉載請自行聯系原作者