iphone游戏开发之cocos2d ( 十 )使用纹理图册,更方便生成精灵动画

来源:互联网 发布:网络维护企业名录 编辑:程序博客网 时间:2024/06/02 23:30

holydancer原创,如需转载,请在显要位置注明:

转自holydancer的CSDN专栏,专栏地址:http://blog.csdn.net/holydancer

在之前的文章七,我们自定义了一个精灵类,该精灵类的init初始化方法中我们使其生成一个动画,并repeatForever,当时我们并没有使用文章九介绍的纹理图册,而是在项目中添加了十几张png图片用作动画中的每一帧,所以我们在生成动画的时候会比较复杂,要转成2D纹理,再循环遍历添加frame数组,再生成动画;文章九我们介绍了纹理图册,将生成动画的每一帧的png图片都放到了同一张大的png中,并有一个plist文件来保存每一帧的CGRect范围等信息,所以我们就不再需要那么麻烦的方式了,cocos2d中有很好的封装类来进行这种操作;

下面来看代码 fishSprite.h:

#import <Foundation/Foundation.h>#import "cocos2d.h"@interface fishSprite : CCSprite {    }+(id)fish;@end

fishSprite.m:

#import "fishSprite.h"@implementation fishSprite+(id)fish{    return [[self alloc]initWithImage];}-(id)initWithImage{    if ((self=[super init])) {                        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"fish.plist"];        // 将所有fish.plist相关的精灵添加到缓存里;                NSMutableArray *frameArray = [NSMutableArray array];                for (int i = 1; i<15; i++) {            [frameArray addObject:             [[CCSpriteFrameCache sharedSpriteFrameCache ]spriteFrameByName:              [NSString stringWithFormat:@"fish%d.png",i]]];        }        //将帧添加到数组里,用该数组生成动画;                CCAnimation *fishAnimation = [CCAnimation animationWithSpriteFrames:frameArray delay:0.05f];                CCAnimate *animate = [CCAnimate actionWithAnimation:fishAnimation];                                                           [self runAction:[CCRepeatForever actionWithAction:animate]];                                                                                        //        NSMutableArray* frames = [NSMutableArray arrayWithCapacity:18];//        //生成一个18的Array;//        //        for (int i = 1; i < 19; i++)//        {//            NSString *pngFile = [NSString stringWithFormat:@"fish%d.png",i];//            //            //利用已知图片名生成纹理;//            CCTexture2D *texture = [[CCTextureCache sharedTextureCache]addImage:pngFile];//            //            //利用纹理生成组成动画的帧;//            CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:texture rect:CGRectMake(0, 0, texture.contentSize.width, texture.contentSize.height)];//            //            //将生成的帧添加到数组中,共18个,之后我们要用这18个frame来构成动画;//            [frames addObject:frame];//            //        }//        //        //利用帧数组生成一个动画,设定帧与帧之间的切换频率为0.05;//        CCAnimation *animation =[CCAnimation animationWithSpriteFrames:frames delay:0.05];//        //        //用CCAnimate将生成的CCAnimation转成可以用精灵操作的动作action://        CCAnimate *animate = [CCAnimate actionWithAnimation:animation];//        //        //设置为repeat//        CCRepeatForever *repeat = [CCRepeatForever actionWithAction:animate];//        //        //执行//        //       //        //        //       //        [self runAction:repeat];//        //这样,如果该精灵一被实例化成功,就会动起来;    }    return self;}@end

在fishSprite.m中,下面注释的代码是之前我们没有使用纹理图册时一个纹理一个纹理添加的代码,另外需要注意的是,昨天我做纹理图册的时候没有注意,只添加了15张图片,完整的是18张,所以for循环中的个数大家知道就行了,纹理图册的循环次数是和plist里面添加的纹理图片的个数相对应的;

这样将fishSprite定义好后,我们在图层上添加时,只需要如下这样调用就能创建一个可以不停动画的精灵;

    //在层上添加精灵;    fishSprite *fish = [fishSprite fish];    fish.position = ccp(160,120);        [self addChild:fish];

这只是纹理图册的一个简单的应用,更常见的是我们将其与CCSpriteBatchNode相结合,使图形的绘制更有效率,我们下次再介绍;