[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];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值