[cocos2d] 利用texture atlases生成动画

 texturepacker可以方便地制作纹理贴图集(Texture Atlases),而且可以免费试用。(可在官网申请免费liscence)

 1 //利用软件将5帧png贴图生成1张大的png贴图和plist
 2    CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
 3 //读取plist并缓存plist包含的贴图
 4     [frameCache addSpriteFramesWithFile:@"sprite.plist"];
 5     NSMutableArray *frames = [NSMutableArray arrayWithCapacity:5];
 6     int i;
 7     for (i = 1; i <= 5; i++){
 8 //假设命名为1.png 2.png。。。。
 9         NSString *file = [NSString stringWithFormat:@"%i.png",i];
10 //从缓存中读取贴图生成动画帧
11         CCSpriteFrame *frame = [frameCache spriteFrameByName:file];
12 //加入到动态数组中         
13         [frames addObject:frame];
14     }
15 //利用动画帧生成动画对象
16    CCAnimation *anim = [CCAnimation animationWithSpriteFrames:frames delay:0.05f];
17 //生成sprite(动画将绑定在sprite上)    
18     CCSprite *sprite = [CCSprite spriteWithSpriteFrame:[frameCache spriteFrameByName:@"1.png"]];
19 //生成在屏幕中间
20     sprite.position = CGPointMake(screenSize.width / 2, screenSize.height / 2);
21 //利用animate运行动画
22     CCAnimate *animate = [CCAnimate actionWithAnimation:anim];
23 //重复播放动画
24     CCRepeatForever *repeat = [CCRepeatForever actionWithAction:animate];
25     [sprite runAction:repeat];
26 //加入到场景中
27     [self addChild:sprite z:0 tag:1];
posted @ 2013-08-22 19:04  CreeperChange  阅读(334)  评论(0编辑  收藏  举报