cocos2d动画用例

// Animation.h

#import"Box2D.h"

#import"cocos2d.h"

#import"BYSingle.h"

@interface AnimSpriteFactory :NSObject {

BYSingle *_single;

BOOL _is4g;

BOOL _isIpad;

}


- (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 {

if((self = [superinit])) {

_single = [BYSingle getInstance];

_is4g =_single.isRetinaSupported;

_isIpad =_single.isIpad;

}

return self;

}


/**

* 用于获取sprite的宽度和高度,太他妈蛋疼了~

*解析plist文件

* 弊端:会使动画第一帧偏低于正常情况(当初不知道是出于什么意图要计算出动画的size)~

*/

-(CGSize) getAnimSpriteSize:(NSString*)animName start:(int)startIndex {

NSString *plistPath = [[NSBundlemainBundle] pathForResource:animNameofType:@"plist"];

NSDictionary *dictionary, *framesDic;

//dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

dictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];

framesDic = [dictionaryobjectForKey:@"frames"];

NSMutableDictionary *startFrameDic = [framesDicobjectForKey:[NSStringstringWithFormat:@"%@%d.png", animName, startIndex]];

NSString *rectDataString = [startFrameDicobjectForKey:@"frame"];

NSRange range =NSMakeRange(1, [rectDataStringlength]-2);

NSString *withoutBorder = [rectDataStringsubstringWithRange:range];

NSArray *array = [withoutBordercomponentsSeparatedByString:@", "];

NSString *sizeString = [arrayobjectAtIndex:1];

NSRange range2 =NSMakeRange(1, [sizeStringlength]-2);

NSString *sizeStringWithoutBorder = [sizeStringsubstringWithRange:range2];

NSArray *size = [sizeStringWithoutBordercomponentsSeparatedByString:@","];

returnCGSizeMake([[size objectAtIndex:0]intValue], [[size objectAtIndex:1]intValue]);

}


// delay默认值为 005~

- (CCSprite*) genAnimSprite:(CGPoint)position

animName:(NSString*)animName

startIndex:(int)startIndex

endIndex:(int)endIndex

repeatForever:(BOOL)repeatForever

delay:(float)delay

{

CCSpriteFrameCache *frameCache = [CCSpriteFrameCachesharedSpriteFrameCache];

// 1.缓冲sprite帧和纹理

NSString *spriteFrameCacheName = [NSStringstringWithFormat:@"%@.plist", animName];

[frameCache addSpriteFramesWithFile:spriteFrameCacheName];

/**

* 2.创建一个精灵批处理结点

* 该步操作放在这儿不好,水动画要用到很多个,这么做的话就会添加进很多重复的 batchNode ~

* Eric 将这步放置在方法的外部,是个很好的做法!

*/

NSString *spriteSheetName = [NSString stringWithFormat:@"%@.png", animName];

CCSpriteBatchNode *spriteBatchNode = [CCSpriteBatchNodebatchNodeWithFile:spriteSheetName];

[_single.gameLayeraddChild:spriteBatchNode];

// 3.收集帧列表

NSMutableArray *frames = [NSMutableArrayarray];

for(int i = startIndex; i < endIndex+1; ++ i) {

NSString *frameName = [NSString stringWithFormat:@"%@%d.png", animName, i];

CCSpriteFrame *spriteFrame = [frameCache spriteFrameByName: frameName];

[frames addObject: spriteFrame];

}

// 4.创建动画对象

CCAnimation *animation = [CCAnimation animationWithFrames:frames delay:delay];

id action = [CCAnimateactionWithAnimation:animation restoreOriginalFrame:NO];

id sequence = nil;

if(repeatForever == YES) {

id repeatAction = [CCRepeatForever actionWithAction:action];

sequence = [CCSequenceactions:repeatAction, nil];

} else {

id callFunc = [CCCallFuncN actionWithTarget:self selector:@selector(removeSprite:)];

sequence = [CCSequenceactions:action, callFunc, nil];

}


// 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) {

// [animSprite setContentSize:size];// 只有这种方法才是有效的~

// } else {

// CGSize ipadSize = CGSizeMake(2*size.width, 2*size.height);

// [animSprite setContentSize:ipadSize];

// }

[animSprite setPosition:position];

/**

*注意:

* 1。先 runAction addChild

* 2addChild方法要由 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

{

CCSpriteFrameCache *frameCache = [CCSpriteFrameCachesharedSpriteFrameCache];

[frameCacheaddSpriteFramesWithFile:[NSStringstringWithFormat:@"%@.plist",actionName]];

NSMutableArray *animFrames = [NSMutableArrayarray];

for(int i =0; i < num ; ++ i) {

NSString *frameName = [NSString stringWithFormat:@"%@%d.png", actionName, i];

[animFramesaddObject:[frameCache spriteFrameByName:frameName]];

}

CCAnimation *anim = [CCAnimationanimationWithFrames:animFrames delay:delay];

CCSprite *sprite = [CCSpritespriteWithSpriteFrameName:[NSStringstringWithFormat:@"%@1.png", actionName]];

[spriterunAction:[CCRepeatForeveractionWithAction:

[CCAnimateactionWithAnimation:anim restoreOriginalFrame:NO]]];

[spriteSheetaddChild:sprite];

return sprite;

}


/** 待气球爆炸动作执行完毕之后,将 animSprite gameLayer中移除~ */

- (void) removeSprite:(id)sender {

CCSprite *animSprite = (CCSprite*)sender;

[_single.gameLayerremoveChild:animSprite cleanup:YES];

}


- (void) dealloc {

[superdealloc];

}


@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值