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 = [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:@"%@.
相关新闻>>
- 发表评论
-
- 最新评论 更多>>