cocos2d中的動畫在每個遊戲當中,都有不少各類各樣的動畫效果,好比街頭霸王中的旋風腿,植物大戰殭屍中豌豆的扭動、殭屍的走路、***動做等,雖然簡單的移動動做也能夠實現這些功能,可是這樣看上去很是的笨拙和不真實。那麼這些效果究竟是如何實現的呢?其實很簡單,咱們只須要將一系列圖片按照特定的順序排列,而後在精靈對象上執行特定的動畫動做就能夠了。
1 cocos2d中動畫相關的類
在cocos2d中實現動畫,須要瞭解如下幾個類。
q
CCAnimate:該類爲一種特殊的動做,也稱爲動畫動做。
q
CCAnimation:該類封裝了一個精靈幀序列和各個精靈幀之間的延遲時間,做爲精靈播放動畫的參數。
q
CCAnimationCache:該類是一個單例,做爲一個緩存池來緩存CCAnimation動畫。
2 簡單動畫效果
接下來咱們經過示例演示在cocos2d中實現動畫效果。在Xcdoe中使用cocos2d模板建立一個項目,命名爲「AnimationTest」,加入準備好的圖片資源,本例爲8張植物大戰殭屍中的殭屍圖片,利用這些單獨的圖片建立動畫,完成一個殭屍走路的動畫效果。實現代碼以下。
數組
程序清單:codes/13/13.11/AnimationTest/AnimationTest/HelloWorldLayer.m緩存
-(id) init
ide
{
工具
if( (self=[super init]) ) {
性能
CGSize winSize = [[CCDirector sharedDirector] winSize];
動畫
CCSprite* bgSprite = [CCSprite spriteWithFile:@"gamebg.png"];
spa
bgSprite.position = ccp(winSize.width/2,winSize.height/2);
設計
[self addChild:bgSprite];
rest
// 建立殭屍精靈,並設置座標位置在屏幕以外
code
CCSprite* zSprite = [CCSprite spriteWithFile:@"z_00_01.png"];
zSprite.position = ccp(winSize.width+zSprite.contentSize.width/2,winSize.height/2);
[self addChild:zSprite];
// 建立CCAnimation動畫,指定動畫幀的內容
CCAnimation* anim = [CCAnimation animation];
[anim addSpriteFrameWithFilename:@"z_00_01.png"];
[anim addSpriteFrameWithFilename:@"z_00_02.png"];
[anim addSpriteFrameWithFilename:@"z_00_03.png"];
[anim addSpriteFrameWithFilename:@"z_00_04.png"];
[anim addSpriteFrameWithFilename:@"z_00_05.png"];
[anim addSpriteFrameWithFilename:@"z_00_06.png"];
[anim addSpriteFrameWithFilename:@"z_00_07.png"];
[anim addSpriteFrameWithFilename:@"z_00_08.png"];
// 建立animAction動畫,restoreOriginalFrame:YES
// 可讓精靈對象在動畫執行完後恢復到最初狀態
id animAction = [CCAnimate actionWithDuration:1.5f animation:anim
restoreOriginalFrame:YES];
// 定義一個動做,重複執行CCAnimate動畫
id repeatanimAction = [CCRepeatForever actionWithAction:animAction];
// 定義一個動做,讓精靈對象移動到特定的位置
id moveTo = [CCMoveTo actionWithDuration:10.0f
position:ccp(-zSprite.contentSize.width/2,winSize.height/2)];
// 殭屍精靈重複執行動畫動做和移動動做
[zSprite runAction:repeatanimAction];
[zSprite runAction:moveTo];
}
return self;
}
-(id) init
{
if( (self=[super init]) ) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
// ①讀取plist文件將精靈幀紋理添加到精靈幀緩存當中
[[CCSpriteFrameCache sharedSpriteFrameCache]
addSpriteFramesWithFile:@"animation.plist"];
// ②建立一個精靈表單
CCSpriteBatchNode* batchNode = [CCSpriteBatchNode
batchNodeWithFile:@"animation.png"];
// ③將精靈表單做爲層的子節點添加到層當中
[self addChild:batchNode];
// ④建立背景精靈添加到精靈表單中
CCSprite* bgSprite = [CCSprite spriteWithSpriteFrameName:@"gamebg.png"];
bgSprite.position = ccp(winSize.width/2,winSize.height/2);
[batchNode addChild:bgSprite];
// ⑤建立殭屍精靈,設置座標位置在屏幕以外
CCSprite* zSprite = [CCSprite spriteWithSpriteFrameName:@"z_00_01.png"];
zSprite.position = ccp(winSize.width+zSprite.contentSize.width,winSize.height/2);
// ⑥建立一個數組用來保存動畫
NSMutableArray* array = [NSMutableArray array];
// 遍歷全部圖片,而後從精靈幀緩存中獲取與圖片名稱相對應的精靈幀保存到數組當中
for(int i = 1;i<=8;i++){
NSString* fileName = [NSString stringWithFormat:@"z_00_0%i.png",i];
CCSpriteFrame* frame = [[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:fileName];
[array addObject:frame];
}
// ⑦建立一個動畫並設計成重複動做
id animation = [CCAnimation animationWithSpriteFrames:array delay:0.1f];
id animate = [CCAnimate actionWithAnimation:animation];
id repeate = [CCRepeatForever actionWithAction:animate];
// ⑧建立一個CCMoveTo讓精靈移動到特定的位置
id moveTo = [CCMoveTo actionWithDuration:10.0f
position:ccp(-zSprite.contentSize.width/2,winSize.height/2)];
// ⑨讓殭屍精靈運行動畫和移動動做
[zSprite runAction:repeate];
[zSprite runAction:moveTo];
// ⑩將殭屍精靈添加到精靈表單中
[bgSprite addChild:zSprite];
}
return self;
}
編號⑨代碼讓殭屍精靈執行動畫和移動的動做。編號⑩代碼將殭屍精靈添加到精靈表單中。
經過以上步驟,咱們就完成了經過精靈表單建立 cocos2d 動畫效果的過程。單擊「 Run 」按鈕執行 AnimationCacheTest 項目,模擬器顯示和直接使用圖片幀建立動畫的效果同樣。使用精靈表單能夠大大提高遊戲的性能,在實際的項目開發當中應該更多地採用精靈表單的方式加載全部的精靈對象。