cocos2d精灵与动画


总结一些精灵与动画操作的方法,其实主要是对CCSpriteFrameCache和CCAnimationCache的理解与使用

  1. [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Jiangshi_Zoulu.plist"];  
  2. //根据一个plist文件名构建CCSpriteFrame对象并添加到内存池中。缺省了Texture文件名的构建方法,缺省的文件名先在_plist中查找,如果没有则查找和plist同名的文件。   
  3. CCAnimation *jiangshi_zoulu=[self animationFromPlist:@"Jiangshi_Zoulu" delay:0.2];  
  4. //根据plist内容生成动画,其实就是根据动画图片的名称生成动画,animationFromPlist函数在后面   
  5. [[CCAnimationCache sharedAnimationCache] addAnimation:jiangshi_zoulu name:@"jiangshi_zoulu"];  
  6. //向池中添加一个CCAnimation对象,索引的key为jiangshi_zoulu   
  7.   
  8. //-------------------------------------------------------------------------------------------分割线   
  9. CCSprite *risex=[CCSprite spriteWithSpriteFrameName:@"risex_0001.png"];  
  10. //根据帧的名字创建一个精灵对象   
  11. CCAnimation *jiangshi=[[CCAnimationCache sharedAnimationCache] animationByName:@"jiangshi_zoulu"];  
  12. //根据key:jiangshi_zoulu从池中获取CCAnimation对象   
  13. id action=[CCAnimate actionWithAnimation:jiangshi];  
  14. //根据CCAnimation生成动画效果CCAnimate   
  15. [risex runAction:[CCRepeatForever actionWithAction:action]];  
  16. //让精灵risex重复执行动画效果   
  17.   
  18. //-------------------------------------------------------------------------------------------分割线   
  19. [[CCAnimationCache sharedAnimationCache] removeAnimationByName:@"jiangshi_zoulu"];  
  20. //根据key从池中删除CCAnimation对象   
  21. [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromFile:@"Jiangshi_Zoulu.plist"];  
  22. //从内存池删除plist中列出的Frame,相当于addSpriteFramesWithFile的逆向操作   
  23.   
  24. //-------------------------------------------------------------------------------------------分割线   
  25. //生成动画函数   
  26. - (CCAnimation *)animationFromPlist:(NSString *)animPlist delay:(float)delay {  
  27.       
  28.     NSString *plistPath = [[NSBundle mainBundle] pathForResource:animPlist ofType:@"plist"]; // 1   
  29.     NSMutableDictionary* dict = [[[NSMutableDictionary alloc] initWithContentsOfFile:plistPath] autorelease];  
  30.     //读取plist内容到字典dict中    
  31.     NSArray *items=[dict valueForKey:@"frames"];  
  32.     int itemcount = [items count];//图片数目即动画长度   
  33.     NSMutableArray *AnimFrames = [NSMutableArray array];  
  34.     //初始化用于存放帧的数组   
  35.     for (int i = 1; i <= itemcount; i++) {  
  36.         NSString *path = [NSString stringWithFormat:@"%@_%04d.png",animPlist,i];  
  37.         //组成动画图片的命名规则是:XXX_0001.png、XXX_0001.png...   
  38.         [AnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:path]];  
  39.         //根据key(图片名)在内存池中查找Frame并存入帧数组中   
  40.     }  
  41.     return [CCAnimation animationWithFrames:AnimFrames delay:delay];//根据帧数组生成动画   
  42. }  
  43.   
  44. //-------------------------------------------------------------------------------------------分割线   
  45. //精灵换图函数   
  46. -(void)changeImageBtn:(CCSprite *)changesprite imagePath:(NSString *)imagePath{  
  47.     CCSpriteFrame* hpframe = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:imagePath];  
  48.     //根据key(图片名)在内存池中查找Frame并返回   
  49.     [changesprite setDisplayFrame:hpframe];//精灵换图   
  50. }  
  51.   
  52. //-------------------------------------------------------------------------------------------分割线   
  53. //CCSpriteBatchNode,它的作用是优化精灵,提高精灵的绘制效率,精灵数量越多,效果越明显。它的工作原理是:将所有该对象的子节点(只能是精灵)用openGL的渲染方法一次性绘制,这样可以省去多次open-close的时间,_作为CCSpriteBatchNode对象子节点的精灵不能用自己的Texture绘制,而是用CCSpriteBatchNode对象的Texture统一绘制,精灵只是提供坐标、旋转角度等信息,这就是它只需要open-close一次的原因,但也正因为如此,导致批处理节点的所有子节点都必须和它用同一套Texture,即CCSpriteBatchNode对象绘制出的图形都是一样的,这点需要格外注意。CCSpriteBatchNode的用法和普通精灵没什么两样,都可以设置Texture,也都是用Texture来绘制,只要通过addChild方法成为其子节点的精灵,都会得到它的优化,但是CCSpriteBatchNode只能添加精灵为子节点。   
  54.   
  55. CCSpriteBatchNode* batch=[CCSpriteBatchNode batchNodeWithFile:@"bullet.pvr.ccz"];  
  56. [self addChild:batch];  
  57. //CCSpriteBatchNode加到层中   
  58. for(int i=0;i<100;i++){  
  59.     CCSprite* sprite=[CCSprite spriteWithFile:@"bullet.png"];  
  60.     [batch addChild:bullet];  
  61.     //添加子节点,子节点必须是精灵,且精灵的Texture和批处理节点相同   
  62. }  

相关内容