生成sprite动画对象的方法
2012-02-17 20:08
204 查看
以下只是一个最原始的方法,从执行效率的角度出发,如果一个场景里面同时包含了100个使用相同动画的sprit,
每个 动画sprite都由下面的方法生成的话,有些代码:
1.缓冲sprite帧和纹理;
2.创建一个精灵节点;
3.收集帧列表;
这些个只需要执行一次的操作便被重复执行了 100次,浪费cpu,浪费电池电量。。
所以说,还是存在相当大的优化空间。。。
- (CCSprite*) genAnimSprite:(CGPoint)position
animName:(NSString*)animName
startIndex:(int)startIndex
endIndex:(int)endIndex
repeatForever:(BOOL)repeatForever
delay:(float)delay
{
// 1.缓冲sprite帧和纹理
NSString *spriteFrameCacheName = [NSStringstringWithFormat:@"%@.plist", animName];
[[CCSpriteFrameCachesharedSpriteFrameCache]
addSpriteFramesWithFile:spriteFrameCacheName];
// 2.创建一个精灵批处理结点
NSString *spriteSheetName = [NSString
stringWithFormat:@"%@.png", animName];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNodebatchNodeWithFile:spriteSheetName];
[_single.gameLayeraddChild:spriteSheet];
// 3.收集帧列表
NSMutableArray *animFrames = [NSMutableArrayarray];
for(int i = startIndex; i < endIndex+1; ++i) {
CCSpriteFrameCache *frameCache = [CCSpriteFrameCachesharedSpriteFrameCache];
NSString *frameName = [NSString
stringWithFormat:@"%@%d.png", animName, i];
CCSpriteFrame *spriteFrame = [frameCache spriteFrameByName: frameName];
[animFrames
addObject: spriteFrame];
}
// 4.创建动画对象
CCAnimation *animation = [CCAnimation
animationWithFrames:animFrames delay:delay];
id action = [CCAnimateactionWithAnimation:animation
restoreOriginalFrame:NO];
id animSequence = nil;
if(repeatForever == YES) {
id repeatAction = [CCRepeatForever
actionWithAction:action];
animSequence = [CCSequenceactions:repeatAction,
nil];
}
else {
id callFunc = [CCCallFuncN
actionWithTarget:self
selector:@selector(removeSprite:)];
animSequence = [CCSequenceactions:action, callFunc,
nil];
}
// 5.创建 sprite
并且让它 run
动画 action~
NSString *str = [NSString
stringWithFormat:@"%@%d.png", animName, startIndex];
CCSprite *animSprite = [CCSprite
spriteWithSpriteFrameName:str];
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];
}
[_single.gameLayeraddChild:animSprite];
[animSprite
setPosition:position];
[animSprite
runAction:animSequence];
return animSprite;
}
/**
待气球爆炸动作执行完毕之后,将 animSprite 从 gameLayer中移除~ */
- (void) removeSprite:(id)sender {
// NSLog(@"回调函数被调用~");
CCSprite *animSprite = (CCSprite*)sender;
[_single.gameLayerremoveChild:animSprite
cleanup:YES];
}
每个 动画sprite都由下面的方法生成的话,有些代码:
1.缓冲sprite帧和纹理;
2.创建一个精灵节点;
3.收集帧列表;
这些个只需要执行一次的操作便被重复执行了 100次,浪费cpu,浪费电池电量。。
所以说,还是存在相当大的优化空间。。。
- (CCSprite*) genAnimSprite:(CGPoint)position
animName:(NSString*)animName
startIndex:(int)startIndex
endIndex:(int)endIndex
repeatForever:(BOOL)repeatForever
delay:(float)delay
{
// 1.缓冲sprite帧和纹理
NSString *spriteFrameCacheName = [NSStringstringWithFormat:@"%@.plist", animName];
[[CCSpriteFrameCachesharedSpriteFrameCache]
addSpriteFramesWithFile:spriteFrameCacheName];
// 2.创建一个精灵批处理结点
NSString *spriteSheetName = [NSString
stringWithFormat:@"%@.png", animName];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNodebatchNodeWithFile:spriteSheetName];
[_single.gameLayeraddChild:spriteSheet];
// 3.收集帧列表
NSMutableArray *animFrames = [NSMutableArrayarray];
for(int i = startIndex; i < endIndex+1; ++i) {
CCSpriteFrameCache *frameCache = [CCSpriteFrameCachesharedSpriteFrameCache];
NSString *frameName = [NSString
stringWithFormat:@"%@%d.png", animName, i];
CCSpriteFrame *spriteFrame = [frameCache spriteFrameByName: frameName];
[animFrames
addObject: spriteFrame];
}
// 4.创建动画对象
CCAnimation *animation = [CCAnimation
animationWithFrames:animFrames delay:delay];
id action = [CCAnimateactionWithAnimation:animation
restoreOriginalFrame:NO];
id animSequence = nil;
if(repeatForever == YES) {
id repeatAction = [CCRepeatForever
actionWithAction:action];
animSequence = [CCSequenceactions:repeatAction,
nil];
}
else {
id callFunc = [CCCallFuncN
actionWithTarget:self
selector:@selector(removeSprite:)];
animSequence = [CCSequenceactions:action, callFunc,
nil];
}
// 5.创建 sprite
并且让它 run
动画 action~
NSString *str = [NSString
stringWithFormat:@"%@%d.png", animName, startIndex];
CCSprite *animSprite = [CCSprite
spriteWithSpriteFrameName:str];
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];
}
[_single.gameLayeraddChild:animSprite];
[animSprite
setPosition:position];
[animSprite
runAction:animSequence];
return animSprite;
}
/**
待气球爆炸动作执行完毕之后,将 animSprite 从 gameLayer中移除~ */
- (void) removeSprite:(id)sender {
// NSLog(@"回调函数被调用~");
CCSprite *animSprite = (CCSprite*)sender;
[_single.gameLayerremoveChild:animSprite
cleanup:YES];
}
相关文章推荐
- 生成sprite动画对象的方法
- javascript生成img标签的3种实现方法(对象、方法、html)
- get set 中 快捷键生成的get方法中 renturn 没有 this.对象 中的this 解决方法
- 在项目中根据配置文件路径生成File对象的方法
- php生成gif动画的方法
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- Eclipse自动生成对象来接收方法的返回值的快捷键
- 对Python中9种生成新对象的方法总结
- javascript 生成img标签的3种方式(对象、方法、html)
- Spring框架bean的注解管理方法之一 使用注解生成对象
- console.log的一个应用 -----用new方法生成一个img对象和document.createElement方法创建一个img对象的区别
- JAVA学习日志 关于调用方法、生成对象的例子。还是用数字卦程序修改
- SAX方式解析XML文件的方法分析,并取特定字段生成对象
- 设计模式之1.1 再谈反射 以及java当中通过配置文件得到类名字,并生成对象的方法
- PHP面向对象程序设计之对象生成方法详解
- 具有默认实参的构造函数的类的对象的生成方法。
- PHP之生成GIF动画的实现方法
- 一种全局对象ID生成方法
- javascript生成对象的三种方法