[?李華明Himi?](http://www.himigame.com/about-himi)原創,轉載務必在明顯處注明:
轉載自[【黑米GameDev街區】](http://www.himigame.com/)?原文鏈接:?[http://www.himigame.com/iphone-cocos2d/525.html](http://www.himigame.com/iphone-cocos2d/525.html "【iOS-Cocos2d游戲開發之二十一")
[](http://blog.csdn.net/xiaominghimi/article/details/6993764)
上周貌似沒有寫新的博文,那么今天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
+(id) mySpriteInitWithFrameName:(NSString*)fileName
{
return [[[self alloc] initWithMySpriteFrameName:fileName] autorelease];//這里仿照cocos2d原理,自動清理精靈
}
-(id) initWithMySpriteFrameName:(NSString*)fileName
{
if ((self = [super initWithSpriteFrameName:fileName]))
{
//初始化的東東都寫在這里喔~
}
return self;
}
-(void) dealloc
{
//內存清理
[super dealloc];
}
@end
~~~
大家注意兩種自定義精靈.m類中的 if((self = XXX)這里是重要的區別,一個是直接索引資源名稱,一個是通過名稱找到幀緩存中的幀;
OK,兩種創建的方式的自定義精靈都完成了,下面我們來嘗試創建吧:
~~~
//---------創建一個我們自定義的MySprite精靈吧(利用文件名直接創建)
//1.import 自定義類.h (#import "MySprite.h")
MySprite*mySprite=[MySprite mySpriteInitWithImage:@"Icon.png"];
mySprite.position=ccp(70,size.height*0.5);
[self addChild:mySprite];
//---------創建一個我們自定義的MySpriteByFrame精靈吧(利用幀緩存中的文件名創建)
//@@@@注意因為是從幀緩存里找到對應名字的幀,那么肯定需要將用到的幀放在緩存里。
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"mySpriteFrames-hd.plist"];
MySpriteByFrame *mySpriteByF =[MySpriteByFrame mySpriteInitWithFrameName:@"himi.png"];
mySpriteByF.position=ccp(300,size.height*0.5);
[self addChild:mySpriteByF];
~~~
注意:利用幀來創建的時候必須要將使用的幀首先加載到幀緩存中,這里Himi利用TP打包工具將一張名字為himi.png的圖打包到mySpriteFrames-hd.plist中了,也就是下面這句代碼將himi.png圖加載到幀緩存中了,否則報錯找不到喔;
~~~
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"mySpriteFrames-hd.plist"];
~~~
加載的圖片資源圖如下:

注意這里的-hd 文件和非-hd的文件的區別,不太懂的童鞋請移步到這篇博文:
[【iOS-Cocos2d游戲開發之九】講解CCSpriteBatchNode與TP工具的".pvr.ccz",".plist"共用的終極精靈優化及注意事項!](http://blog.csdn.net/xiaominghimi/article/details/6761811)
運行截圖如下:

下面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
// 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仍然封裝成一個方法讓你直接使用即可;首先看下代碼:
~~~
//帶有攻擊幀的動畫
+(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源碼進行的擴展!**
[http://www.himigame.com/iphone-cocos2d/525.html](http://www.himigame.com/iphone-cocos2d/525.html "【iOS-Cocos2d游戲開發之二十一")
- 前言
- 【Iphone 游戲開發】游戲引擎剖析
- [Object-C語言隨筆之一]Mac os 下搭建iOS開發環境
- [Object-C語言隨筆之二] 《NSLog》常用的打印調試語句與自動排版
- [Object-C語言隨筆之三] 類的創建和實例化以及函數的添加和調用!
- [Object-C語言隨筆之四]創建視圖并繪制簡單圖形
- 【iOS-Cocos2d游戲開發之一】搭建cocos2d游戲引擎環境HelloWorld!
- 【iOS-Cocos2d游戲開發之二】Cocos2D 游戲開發資源貼(教程以及源碼)
- 【iOS-Cocos2d游戲開發之三】CCScene切換的所有特效(28種)以及設置屏幕橫豎屏!
- 【iOS-Cocos2d游戲開發之四】獨自收集Cocos2d提供的字體!共57種(有對照的字體圖)
- 【iOS-Cocos2d游戲開發之五】多觸點與觸屏事件詳解(單一監聽、事件分發)【11月28日更新】
- 【iOS-Cocos2d游戲開發之六】對觸屏事件追加講解,解決無法觸發ccTouchMoved事件[重要!]
- 【iOS-Cocos2d游戲開發之七】在cocos2d中添加/刪除系統組件,并解決View設置透明會影響View中的其他組件的問題!【11月28日更新解決添加組件Cocos2d動畫停止播放的BUG】
- 【iOS-Cocos2d游戲開發之八】開啟高清(960*640)模式問題與解答、圖片適配以及設置iphone橫豎屏
- 【iOS-Cocos2d游戲開發之九】講解CCSpriteBatchNode與TP工具的&quot;.pvr.ccz&quot;,&quot;.plist&quot;共用的終極精靈優化及注意事項!
- 【iOS-Cocos2d游戲開發之十】添加粒子系統特效并解決粒子特效與Layer之間的坐標問題;
- 【iOS-Cocos2d游戲開發之十一】使用Box2d物理系統以及在cocos2d框架添加Box2d物理系統lib包的方法
- 【iOS-Cocos2d游戲開發之十二】淺析使用C++/C/OC進行iOS游戲混編出現“failed with exit”問題與小結;
- 【iOS-Cocos2d游戲開發之十三】CCSprite利用Bezier(貝塞爾)做拋物線動作并讓CCSprite同時播放兩個Action動作!
- 【iOS-Cocos2d游戲開發之十四】音頻/音效/視頻播放(利用Cocos2D-iPhone-Extensions嵌入Cocos2d進行視頻播放!)
- 【iOS-Cocos2d游戲開發之十五】詳解CCProgressTimer 進度條并修改cocos2d源碼實現“理想”游戲進度條!
- 【iOS-Cocos2d游戲開發之十六】添加本地通知(UILocalNotification)以及添加系統組件滾動視圖(UIScrollView)!【2011年11月15日更新】
- 【iOS-Cocos2d游戲開發之十七】靈活使用精靈可視區域(TextureRect)與錨點(anchorPoint),并結合可視區域與錨點制作進度條!
- 【iOS開發必備指南合集】申請企業級IDP、真機調試、游戲接入GameCenter 指南(實現仿官方的成就提示)、游戲接入OpenFeint指南;
- 【iOS-Cocos2d游戲開發之十八】解決滾屏背景/拼接地圖有黑邊(縫隙)/動畫播放出現毛邊以及禁止游戲中自動鎖屏問題!【2011年12月18日補充】
- 【iOS開發必收藏】詳解iOS應用程序內使用IAP/StoreKit付費、沙盒(SandBox)測試、創建測試賬號流程!【2012-12-11日更新獲取&quot;產品付費數量等于0的問題&quot;】
- 【iOS-cocos2d-X 游戲開發之一】在Mac下結合Xcode搭建Cocos2d-X開發環境!
- 【iOS-cocos2d-X 游戲開發之二】【必看篇】總結闡述Cocos2d-X與Cocos2d-iphone區別;
- 【iOS-Cocos2d游戲開發之十九】游戲數據存儲的四種常用方式NSKeyedArchiver/NSUserDefaults/Write寫入/SQLite3
- 【iOS-Cocos2d游戲開發之二十】精靈的基礎知識點總匯(位圖操作/貼圖更換/重排z軸等)以及利用CCSprite與CCLayerColor制作簡單遮蓋層!
- 【iOS-Cocos2d游戲開發之二十一 】自定義精靈類并為你的精靈設置攻擊幀(指定開始幀)以及擴展Cocos2d源碼的CCAnimation簡化動畫創建!
- 【iOS-Cocos2d游戲開發之二十二 】CCSpeed實現CCAnimate動畫進行時設置慢動作以及設置游戲加減速進行(塔防游戲必備)!
- 【iOS-cocos2d-X 游戲開發之三】Mac下配置Android NDK環境并搭建Cocos2d-x環境并Eclipse正常編譯運行Cocos2dX自帶TestsDemo項目!
- 【iOS-cocos2d-X 游戲開發之四】Cocos2dX創建Android NDK新項目并編譯導入Eclipse中正常運行!
- 【iOS-cocos2d-X 游戲開發之五】游戲存儲之Cocos2dX自帶CCUserDefault類詳解;
- 【iOS-cocos2d-X 游戲開發之六】使用Base64算法對Cocos2dX自帶CCUserDefault游戲存儲數據編碼!
- 【iOS-cocos2d-X 游戲開發之七】整合Cocos2dX的Android項目到Xcode項目中,Xcode編寫&amp;編譯代碼,Android導入打包運行即可!
- 【iOS-iap防護】驗證用戶付費收據!拒絕iap Cracker!拒絕iap Free!讓iphone越獄用戶無從下手!【2012年5月2日更新防護iap Free的方法】
- 【COCOS2DX-LUA 腳本開發之一】在Cocos2dX游戲中使用Lua腳本進行游戲開發(基礎篇)并介紹腳本在游戲中詳細用途!
- 【iOS-cocos2d-X 游戲開發之九】Cocos2dx利用CCSAXParser解析xml數據&amp;CCMutableDictionary使用與注意!
- 【iOS-cocos2d-X 游戲開發之十】自定義CCSprite/Layer/CCNode及靜態類模版&amp;自定義類細節說明&amp;Cocos2dx觸屏事件講解
- 【iOS-cocos2d-X 游戲開發之十一】New CCSprite()帶來的錯誤&amp;使用CCUserDefault及pvr.ccz在Cocos2dx中要注意!
- 【iOS-cocos2d-X 游戲開發之十二】自定義Cocos2dx搖桿(增強Joystick),增加搖桿跟隨用戶觸點作為搖桿坐標,讓搖桿不再死板!
- 【iOS-cocos2d-X 游戲開發之十三】詳細講解在Xcode中利用預編譯并通過Jni調用Android的Java層代碼(cocos2dx里訪問調用Android函數)!
- 【iOS-cocos2d-X 游戲開發之十四】Xcode中c++&amp;Object-C混編,詳細介紹如何在cocos2dx中訪問object函數以及Apple Api
- 【iOS-cocos2d-X 游戲開發之十五】Cocos2dx中響應Android的Back(返回)與Menu(小房子)事件&amp;&amp;Cocos2dx自動釋放粒子內存函數!
- 【iOS-cocos2d-X 游戲開發之十六】配置你的Cocos2dx項目編譯后的Android自動使用(-hd)高清圖&amp;設置Android自適應屏幕、縮放比例方法!
- 【Cocoa(mac) Application 開發系列之四】動作編輯器(Cocos2dx)制作流程詳解及附上響應鼠標滾軸事件、反轉坐標系、導入/創建資源目錄等知識點代碼!
- 【Cocos2d-X(2.x) 游戲開發系列之一】cocos2dx(v2.x)與(v1.x)的一些常用函數區別講解!在2.x版CCFileData類被去除等
- 【Cocos2d-X(2.x) 游戲開發系列之二】cocos2dx最新2.0.1版本跨平臺整合NDK+Xcode,Xcode編寫&amp;編譯代碼,Android導入打包運行即可!
- 【Cocos2dX(2.x)_Lua開發之三】★重要必看篇★在Lua中使用自定義精靈(Lua腳本與自創建類之間的訪問)及Lua基礎講解
- 【Cocos2d-X(2.x) 游戲開發系列之三】最新版本cocos2d&#173;2.0&#173;x&#173;2.0.2使用新資源加載策略!不再沿用-hd、-ipad、-ipadhd添加后綴方式
- 【Cocos2d-X(1.x 2.x) 修復篇】iOS6 中 libcurl.a 無法通過armv7s編譯以及iOS6中無法正常游戲橫屏的解決方法
- 【Cocos2d-X(1.x 2.x) 】iOS6與iphone5適相關設置隨筆(解決第三方類庫無法通過armv7s編譯的方法、添加Default-568h@2x.png)