生成sprite动画对象的方法

以下只是一个最原始的方法,从执行效率的角度出发,如果一个场景里面同时包含了100个使用相同动画的sprit,

每个 动画sprite都由下面的方法生成的话,有些代码:

1.缓冲sprite帧和纹理;

2.创建一个精灵节点;

3.收集帧列表;

这些个只需要执行一次的操作便被重复执行了 100次,浪费cpu,浪费电池电量。。

所以说,还是存在相当大的优化空间。。。


- (CCSprite*) genAnimSprite:(CGPoint)position 

                   animName:(NSString*)animName 

                 startIndex:(int)startIndex 

                   endIndex:(int)endIndex 

              repeatForever:(BOOL)repeatForever 

                      delay:(float)delay 

{

   // 1.缓冲sprite帧和纹理

    NSString *spriteFrameCacheName = [NSStringstringWithFormat:@"%@.plist", animName];

    [[CCSpriteFrameCachesharedSpriteFrameCache] addSpriteFramesWithFile:spriteFrameCacheName];

    

    // 2.创建一个精灵批处理结点

    NSString *spriteSheetName = [NSString stringWithFormat:@"%@.png", animName];

    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNodebatchNodeWithFile:spriteSheetName];

    [_single.gameLayeraddChild:spriteSheet];

    

   // 3.收集帧列表

   NSMutableArray *animFrames = [NSMutableArrayarray];

    for(int i = startIndex; i < endIndex+1; ++i) {

       CCSpriteFrameCache *frameCache = [CCSpriteFrameCachesharedSpriteFrameCache];

        NSString *frameName = [NSString stringWithFormat:@"%@%d.png", animName, i];

        CCSpriteFrame *spriteFrame = [frameCache spriteFrameByName: frameName];

        [animFrames addObject: spriteFrame];

    }

    

   // 4.创建动画对象

    CCAnimation *animation = [CCAnimation animationWithFrames:animFrames delay:delay];

   id action = [CCAnimateactionWithAnimation:animation restoreOriginalFrame:NO];

    id animSequence = nil;

    if(repeatForever == YES) {

        id repeatAction = [CCRepeatForever actionWithAction:action];

        animSequence = [CCSequenceactions:repeatAction, nil];

    } else {

        id callFunc = [CCCallFuncN actionWithTarget:self selector:@selector(removeSprite:)];

        animSequence = [CCSequenceactions:action, callFunc, nil];

    }


   // 5.创建 sprite 并且让它 run 动画 action~

    NSString *str = [NSString stringWithFormat:@"%@%d.png", animName, startIndex];

    CCSprite *animSprite = [CCSprite spriteWithSpriteFrameName:str];

    CGSize size = [self getAnimSpriteSize:animName start:startIndex];

    if(!_isIpad) {

        [animSprite setContentSize:size]; //只有这种方法才是有效的~

    } else {

        CGSize ipadSize = CGSizeMake(2*size.width,2*size.height);

        [animSprite setContentSize:ipadSize];

    }

    [_single.gameLayeraddChild:animSprite];

    [animSprite setPosition:position];

    [animSprite runAction:animSequence];

    

    return animSprite;

}


/** 待气球爆炸动作执行完毕之后,将 animSprite gameLayer中移除~ */

- (void) removeSprite:(id)sender {

//    NSLog(@"回调函数被调用~");

    CCSprite *animSprite = (CCSprite*)sender;

    [_single.gameLayerremoveChild:animSprite cleanup:YES];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值