cocos2d中的動畫在每一個游戲當中,都有很多各種各樣的動畫效果,比如街頭霸王中的旋風腿,植物大戰(zhàn)僵尸中豌豆的扭動、僵尸的走路、***動作等,雖然簡單的移動動作也可以實現(xiàn)這些功能,但是這樣看上去非常的笨拙和不真實。那么這些效果到底是如何實現(xiàn)的呢?其實很簡單,我們只需要將一系列圖片按照特定的順序排列,然后在精靈對象上執(zhí)行特定的動畫動作就可以了。
1 cocos2d中動畫相關的類
在cocos2d中實現(xiàn)動畫,需要了解以下幾個類。 q CCAnimate:該類為一種特殊的動作,也稱為動畫動作。 q CCAnimation:該類封裝了一個精靈幀序列和各個精靈幀之間的延遲時間,作為精靈播放動畫的參數(shù)。 q CCAnimationCache:該類是一個單例,作為一個緩存池來緩存CCAnimation動畫。
2 簡單動畫效果
接下來我們通過示例演示在cocos2d中實現(xiàn)動畫效果。在Xcdoe中使用cocos2d模板創(chuàng)建一個項目,命名為“AnimationTest”,加入準備好的圖片資源,本例為8張植物大戰(zhàn)僵尸中的僵尸圖片,利用這些單獨的圖片創(chuàng)建動畫,完成一個僵尸走路的動畫效果。實現(xiàn)代碼如下。
創(chuàng)新互聯(lián)主要從事成都網(wǎng)站設計、網(wǎng)站制作、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務鄂倫春,10年網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18980820575
程序清單:codes/13/13.11/AnimationTest/AnimationTest/HelloWorldLayer.m
-(id) init
{
if( (self=[super init]) ) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite* bgSprite = [CCSprite spriteWithFile:@"gamebg.png"];
bgSprite.position = ccp(winSize.width/2,winSize.height/2);
[self addChild:bgSprite];
// 創(chuàng)建僵尸精靈,并設置坐標位置在屏幕之外
CCSprite* zSprite = [CCSprite spriteWithFile:@"z_00_01.png"];
zSprite.position = ccp(winSize.width+zSprite.contentSize.width/2,winSize.height/2);
[self addChild:zSprite];
// 創(chuàng)建CCAnimation動畫,指定動畫幀的內(nèi)容
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"];
// 創(chuàng)建animAction動畫,restoreOriginalFrame:YES
// 可以讓精靈對象在動畫執(zhí)行完后恢復到最初狀態(tài)
id animAction = [CCAnimate actionWithDuration:1.5f animation:anim
restoreOriginalFrame:YES];
// 定義一個動作,重復執(zhí)行CCAnimate動畫
id repeatanimAction = [CCRepeatForever actionWithAction:animAction];
// 定義一個動作,讓精靈對象移動到特定的位置
id moveTo = [CCMoveTo actionWithDuration:10.0f
position:ccp(-zSprite.contentSize.width/2,winSize.height/2)];
// 僵尸精靈重復執(zhí)行動畫動作和移動動作
[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"];
// ②創(chuàng)建一個精靈表單
CCSpriteBatchNode* batchNode = [CCSpriteBatchNode
batchNodeWithFile:@"animation.png"];
// ③將精靈表單作為層的子節(jié)點添加到層當中
[self addChild:batchNode];
// ④創(chuàng)建背景精靈添加到精靈表單中
CCSprite* bgSprite = [CCSprite spriteWithSpriteFrameName:@"gamebg.png"];
bgSprite.position = ccp(winSize.width/2,winSize.height/2);
[batchNode addChild:bgSprite];
// ⑤創(chuàng)建僵尸精靈,設置坐標位置在屏幕之外
CCSprite* zSprite = [CCSprite spriteWithSpriteFrameName:@"z_00_01.png"];
zSprite.position = ccp(winSize.width+zSprite.contentSize.width,winSize.height/2);
// ⑥創(chuàng)建一個數(shù)組用來保存動畫
NSMutableArray* array = [NSMutableArray array];
// 遍歷所有圖片,然后從精靈幀緩存中獲取與圖片名稱相對應的精靈幀保存到數(shù)組當中
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];
}
// ⑦創(chuàng)建一個動畫并設計成重復動作
id animation = [CCAnimation animationWithSpriteFrames:array delay:0.1f];
id animate = [CCAnimate actionWithAnimation:animation];
id repeate = [CCRepeatForever actionWithAction:animate];
// ⑧創(chuàng)建一個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;
}
編號⑨代碼讓僵尸精靈執(zhí)行動畫和移動的動作。編號⑩代碼將僵尸精靈添加到精靈表單中。
通過以上步驟,我們就完成了通過精靈表單創(chuàng)建cocos2d動畫效果的過程。單擊“Run”按鈕執(zhí)行AnimationCacheTest項目,模擬器顯示和直接使用圖片幀創(chuàng)建動畫的效果一樣。使用精靈表單可以大大提升游戲的性能,在實際的項目開發(fā)當中應該更多地采用精靈表單的方式加載所有的精靈對象。
網(wǎng)站標題:來源于一般討論分類瘋狂ios講義之cocos2d中的動畫
本文URL:http://www.chinadenli.net/article22/jdjgcc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供面包屑導航、網(wǎng)站設計公司、網(wǎng)站導航、外貿(mào)網(wǎng)站建設、軟件開發(fā)、手機網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)