转载]cocos2d中CCAnimation的使用(cocos2d 1.0以上版本)

来源:互联网 发布:js math 向上取整 编辑:程序博客网 时间:2024/06/07 06:56
在cocos2d 
0.9及以下版本中,CCAnimation中可以使用animationWithName,但在1.0及以上版本中,此方法已经不能使用了,而是使用animationWithFrames替代,而  animationWithFrames的参数是一个NSArray类型的数据,所以要经过一些转换后才能实现,具体实现参照如下例子:

      CCTexture2D *texture = [[CCTextureCachesharedTextureCache] addImage:@"***.png"];

      CCSpriteFrame *frame = [CCSpriteFrameframeWithTexture:texture rect:CGRectMake(0, 0,texture.contentSize.width,texture.contentSize.height)];

     NSArray *array =[[NSArray alloc]initWithObjects:frame,nil];

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

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

       {

          int x = i % 3;

          [animation addFrameWithTexture:mgr.texturerect:CGRectMake(x*32, 0, 31, 30)];

       }

       id action = [CCAnimateactionWithAnimation:animation];

      [flight runAction:[CCRepeatForeveractionWithAction:action]];

    此处的fight是CCSprite类型的变量