cocos2d動畫用例

2021-08-26 14:01:56 字數 2832 閱讀 7126

// animation.h

#import"box2d.h"

#import"cocos2d.h"

#import"bysingle.h"

@inte***ce animspritefactory :nsobject

- (id) init;

- (ccsprite*) genanimsprite:(cgpoint)position

animname:(nsstring*)animname

startindex:(int)startindex

endindex:(int)endindex

repeatforever:(bool)repeatforever

delay:(float)delay;

- (void) dealloc;

@end

// animation.mm

#import"animspritefactory.h"

@implementation animspritefactory

- (id) init

returnself; }

/**

* 用於獲取sprite的寬度和高度,太他媽蛋疼了~

*解析plist檔案

* 弊端:會使動畫第一幀偏低於正常情況(當初不知道是出於什麼意圖要計算出動畫的size)~

*/-(cgsize) getanimspritesize:(nsstring*)animname start:(int)startindex

// delay預設值為 0。05~

- (ccsprite*) genanimsprite:(cgpoint)position

animname:(nsstring*)animname

startindex:(int)startindex

endindex:(int)endindex

repeatforever:(bool)repeatforever

delay:(float)delay

// 4.建立動畫物件

ccanimation *animation = [ccanimation

animationwithframes:frames delay:delay];

idaction = [

ccanimate

actionwithanimation:animation restoreoriginalframe:

no];

id sequence = nil;

if(repeatforever == yes) else

// 5.建立 sprite 並且讓它 run 動畫 action~

nsstring *fristframename = [nsstring

stringwithformat:@"%@%d.png", animname, startindex];

ccsprite *animsprite = [ccsprite

spritewithspriteframename:fristframename];

// cgsize size = [self getanimspritesize:animname start:startindex];

// if(!_isipad) else

[animsprite setposition:position];

/**

*注意:

* 1。先 runaction再 addchild,

* 2。addchild方法要由 ccspritebatchnode 物件呼叫,而不要由 _single.gamelayer 呼叫~

*由後者呼叫的話就失去了使用 batchnode 的意義~

* (建立乙個精靈批處理結點)

*/[animsprite runaction:sequence];

[spritebatchnodeaddchild:animsprite];

return animsprite;

}// created by eric~

-(ccsprite*) createanimation:(nsstring*)actionname

delay:(float)delay

framecount:(int)num

banchnode:(ccspritebatchnode*)spritesheet

ccanimation *anim = [ccanimation

animationwithframes:animframes delay:delay];

ccsprite *sprite = [ccsprite

spritewithspriteframename:[nsstring

stringwithformat:@"%@1.png", actionname]];

[spriterunaction:[

ccrepeatforever

actionwithaction: [

ccanimate

actionwithanimation:anim restoreoriginalframe:

no]]];

[spritesheetaddchild:sprite];

return sprite;

}/** 待氣球**動作執行完畢之後,將 animsprite 從 gamelayer中移除~ */

- (void) removesprite:(id)sender

- (void) dealloc

@end

cocos2d動畫用例

animation.h import box2d.h import cocos2d.h import bysingle.h inte ce animspritefactory nsobject id init ccsprite genanimsprite cgpoint position animn...

cocos2d筆記 cocos2d的單例模式

cocos2d充分使用了單例設計模式,這裡有必要提及它是因為它是經常是討論熱點。大體上說,乙個singleton是乙個類,它在應用使用期中僅僅例項化一次。為了確保如此,就要使用乙個既建立又訪問此物件例項的靜態方法。因此,你不使用alloc init或者靜態自動釋放初始化器 static autore...

cocos2d實現語音 Cocos2d 聲音API

param url 聲音路徑 cc.audioengine.playmusic url loop 停止背景 param releasedata 是否釋放聲音資料,預設為false cc.audioengine.stopmusic releasedata 暫停背景 cc.audioengine.pau...