cocos2d动画用例
// Animation.h
#import "Box2D.h"
#import "cocos2d.h"
#import "BYSingle.h"
@inte易做图ce 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 = [super init])) {
_single = [BYSingle getInstance];
_is4g = _single.isRetinaSupported;
_isIpad = _single.isIpad;
}
return self;
}
/**
* 用于获取 sprite 的宽度和高度,太他妈蛋疼了~
* 解析 plist 文件
* 弊端:会使动画第一帧偏低于正常情况(当初不知道是出于什么意图要计算出动画的 size)~
*/
-(CGSize) getAnimSpriteSize:(NSString*)animName start:(int)startIndex {
NSString *plistPath = [[NSBundle mainBundle] pathForResource:animName ofType:@"plist"];
NSDictionary *dictionary, *framesDic;
//
dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
dictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
framesDic = [dictionary objectForKey:@"frames"];
NSMutableDictionary *startFrameDic = [framesDic objectForKey:[NSString stringWithFormat:@"%@%d.png", animName, startIndex]];
NSString *rectDataString = [startFrameDic objectForKey:@"frame"];
NSRange range = NSMakeRange(1, [rectDataString length]-2);
NSString *withoutBorder = [rectDataString substringWithRange:range];
NSArray *array = [withoutBorder componentsSeparatedByString:@", "];
NSString *sizeString = [array objectAtIndex:1];
NSRange range2 = NSMakeRange(1, [sizeString length]-2);
NSString *sizeStringWithoutBorder = [sizeString substringWithRange:range2];
NSArray *size = [sizeStringWithoutBorder componentsSeparatedByString:@","];
return CGSizeMake([[size objectAtIndex:0] intValue], [[size objectAtIndex:1] intValue]);
}
// delay 默认值为 0。05~
- (CCSprite*) genAnimSprite:(CGPoint)position
animName:(NSString*)animName
startIndex:(int)startIndex
endIndex:(int)endIndex
repeatForever:(BOOL)repeatForever
delay:(float)delay
{
CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
// 1.缓冲sprite帧和纹理
NSString *spriteFrameCacheName = [NSString stringWithFormat:@"%@.plist", animName];
[frameCache addSpriteFramesWithFile:spriteFrameCacheName];
/**
* 2.创建一个精灵批处理结点
* 该步操作放在这儿不好,水动画要用到很多个,这么做的话就会添加进很多重复的 batchNode ~
* Eric 将这步放置在方法的外部,是个很好的做法!
*/
NSString *spriteSheetName = [NSString stringWithFormat:@"%@.png", animName];
CCSpriteBatchNode *spriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:spriteSheetName];
[_single.gameLayer addChild:spriteBatchNode];
// 3.收集帧列表
NSMutableArray *frames = [NSMutableArray array];
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 = [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO];
id sequence = nil;
if(repeatForever == YES) {
id repeatAction = [CCRepeatForever actionWithAction:action];
sequence = [CCSequence actions:repeatAction, nil];
} else {
id callFunc = [CCCallFuncN actionWithTarget:self selector:@selector(removeSprite:)];
sequence = [CCSequence actions: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,
* 2。addChild 方法要由 CCSpriteBatchNode 对象调用,而不要由 _single.gameLayer 调用~
* 由后者调用的话就失去了使用 batchNode 的意义~
* (创建一个精灵批处理结点)
*/
[animSprite runAction:sequence];
[spriteBatchNode addChild:animSprite];
return animSprite;
}
// Created by Eric~
-(CCSprite*) createAnimation:(NSString*)actionName
&nb
补充:移动开发 , IOS ,