【iOS-Cocos2d游戏开发】精灵缓存和动画缓存

合理使用CCAnimationCache动画缓存和CCSpriteFrameCache精灵帧缓存


 如果有一大批CCSprite要操作,缓存到Cache中比每次都创建要高效很多。

例子:射击游戏,TD 等等


缓存grasslands.plist精灵帧文件(加入)

[[CCSpriteFrameCache sharedSpriteFrameCacheaddSpriteFramesWithFile:@"grasslands.plist"];


取出缓冲帧中的zhang_1.png这一精灵帧(取出)

[[CCSpriteFrameCache sharedSpriteFrameCachespriteFrameByName:@"zhang_1.png"];



//利用帧缓存中的帧名称

+(CCAnimation*) animationWithFrame:(NSString*)frame frameCount:(int)frameCount delay:(float)delay

{

    NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];

    NSString* file;  

    

    for (int i =0; i < frameCount; i++)

    {

        file =nil;

        file = [NSStringstringWithFormat:@"%@_%i.png", frame, i];

        CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];


        CCSpriteFrame* frame = [frameCachespriteFrameByName:file];

        [frames addObject:frame];

    }

    return  [CCAnimation animationWithFrames:framesdelay:delay];

}



//通过上述方法建立一个动画

CCAnimation *animation=[CCAnimation animationWithFrame:self.frameNamedelay:0.04fframeIndex:i];




//缓存这个动画 名字叫zhang_1(加入)

 [[CCAnimationCache sharedAnimationCacheaddAnimation:animationname:@"zhang_1"];


//取出这个动画文件(取出动画)

CCAnimation *animation=[[CCAnimationCache sharedAnimationCache] animationByName@"zhang_1"];


通过缓存的模式 可以大大提高内存使用率


你可能感兴趣的:(游戏,cache,File,animation,float,delay)