您的位置:首页 > 移动开发 > Cocos引擎

cocos2d中如何使用图片纹理图集的加载来实现一个动画的功能

2013-10-24 12:33 931 查看
cocos2d中要实现一个动画,一般采用纹理图集的方式,也就是说把几个连续动作的图片挨个显示切换这样就是动画

一: 首先先看下今天要实现的具体的目的,打飞机的时间屏幕上会有一个喷火的小飞机,飞机的尾部会有喷火 熄灭 在开始喷火的动画

今天就实现这个场景动画,首先看下素材



我们的目的就是把它实现成一个三个图片连续切换动画



类似这种样式

二: 首先,我们需要创建一个精灵批处理集合对象

flightSheet = [CCSpriteBatchNodebatchNodeWithFile:@"flight.png"capacity:3]; //这个位置的图片就是三个额小飞机的图片

flight = [CCSpritespriteWithTexture:flightSheet.texturerect:CGRectMake(0, 0, 31, 30)];

flight.scale = 1.4;

flight.position = ccp(size.width/2, size.height -450);

[flightSheetaddChild:flight];

[self addChild:flightSheet];

NSMutableArray* array = [NSMutableArrayarray];

for (int i = 0; i<3; i++) {

[array addObject:[CCSpriteFrame frameWithTexture:flightSheet.texture rect:CGRectMake(i*32, 0, 31, 30)]];

//注意这个地方我只是实用的手动计算具体的图片在整个大图片集合上的位置,大家可以使用cocos2d专门的图片处理工具

}

CCAnimation *animation = [CCAnimation animationWithSpriteFrames:array delay:0.2f];

CCAnimate* animate = [CCAnimate actionWithAnimation:animation];

CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];

[flight runAction:repeat];

执行这个事件动作,OK

动画成功!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: