cocos2d教程 —— 播放动画
2011-07-29 18:43
337 查看
标签: sheet 2d che 杂谈 | 分类: cocos2d |
播放动画代码如下:
- (void) playAnimation {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"animation.plist"];
CCSpriteSheet *sheet = [CCSpriteSheet spriteSheetWithFile:@"animation.png"];
CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"Target1.png"];
sprite.position = monster.position; //设置动画播放位置
[self addChild:sheet z:1 tag:100]; //设置tag为了在后面的函数中读取sheet
[sheet addChild:sprite];
NSMutableArray *animationFrames = [NSMutableArray array];//存储动画序列
for(int i = 1; i != 18; i++){
NSString *frameName = [NSString stringWithFormat:@"Target%d.png",i];
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName];
[animationFrames addObject:frame];
}
CCAnimation *animation = [CCAnimation animationWithName:@"monster dead" delay:0.04f frames:animationFrames];
[sprite runAction:[CCSequence actions:
[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO],
[CCCallFunc actionWithTarget:self selector:@selector(removeAnimation)],nil]];
}
// 添加一个清除函数是因为动画的最后一帧不会消失,如果有更好的处理办法,给我留言
- (void) removeAnimation {
CCSpriteSheet *sheet = (CCSpriteSheet *)[self getChildByTag:100];
[self removeChild:sheet cleanup:YES];
}
cocos2d文档中说道
The sprite frames will be cached AND RETAINED, and they won't be released unless you call // [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
但是我在自己的程序中得出的是sprites frames没有保留(retain),所以在dealloc中添加上面的代码会出错。如果有知道原因的,给我留言。
相关文章推荐
- 【iOS-Cocos2d游戏开发之七】在cocos2d中添加/删除系统组件,并解决View设置透明会影响View中的其他组件的问题!【11月28日更新解决添加组件Cocos2d动画停止播放的BUG】
- cocos2d-x教程-CCAnimationa创建动画
- CCAnimation创建动画cocos2d-x教程
- Cocos2d-x中子ccb动画无法正常播放的问题
- cocos2d-x中两种播放精灵动画的方式
- cocos2d循环播放动画
- cocos2d 0.99 与cocos2d v2.1 restoreOriginalFrame 播放动画使用重置默认桢的区别
- cocos2d-x播放骨骼动画
- Cocos2d-3.x_创建3D精灵,播放模型动画
- cocos2d-js 3.0 rc2 自定义UI控件组件 例子:能播放动画的MenuItem。MenuItemSprite的bug
- Cocos2d-x 3.0final 终结者系列教程22Cocos Studio动画编辑器的帧动画和骨骼动画详解
- cocos2d3.3不能播放cocostudio导出的粒子动画
- Swift开发教程--如何播放图片动画
- 关于cocos2d-x播放动画时获取第几帧的另一种方法
- Cocos2d播放Cocostudio编辑动画
- cocos2d播放动画完后回到第一帧的图的方法
- Cocos2d-x教程(4)-创建动画效果,TexturePacker工具
- cocos2d-x 3.0 帧播放 (帧动画循环播放)
- 手机上播放动画教程(一)
- cocos2d+lua实现帧动画播放