【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"];


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


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值