歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> cocos2d精靈與動畫

cocos2d精靈與動畫

日期:2017/3/1 10:13:17   编辑:Linux編程

總結一些精靈與動畫操作的方法,其實主要是對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. CCSprite *risex=[CCSprite spriteWithSpriteFrameName:@"risex_0001.png"];
  9. //根據幀的名字創建一個精靈對象
  10. CCAnimation *jiangshi=[[CCAnimationCache sharedAnimationCache] animationByName:@"jiangshi_zoulu"];
  11. //根據key:jiangshi_zoulu從池中獲取CCAnimation對象
  12. id action=[CCAnimate actionWithAnimation:jiangshi];
  13. //根據CCAnimation生成動畫效果CCAnimate
  14. [risex runAction:[CCRepeatForever actionWithAction:action]];
  15. //讓精靈risex重復執行動畫效果
  16. //-------------------------------------------------------------------------------------------分割線
  17. [[CCAnimationCache sharedAnimationCache] removeAnimationByName:@"jiangshi_zoulu"];
  18. //根據key從池中刪除CCAnimation對象
  19. [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromFile:@"Jiangshi_Zoulu.plist"];
  20. //從內存池刪除plist中列出的Frame,相當於addSpriteFramesWithFile的逆向操作
  21. //-------------------------------------------------------------------------------------------分割線
  22. //生成動畫函數
  23. - (CCAnimation *)animationFromPlist:(NSString *)animPlist delay:(float)delay {
  24. NSString *plistPath = [[NSBundle mainBundle] pathForResource:animPlist ofType:@"plist"]; // 1
  25. NSMutableDictionary* dict = [[[NSMutableDictionary alloc] initWithContentsOfFile:plistPath] autorelease];
  26. //讀取plist內容到字典dict中
  27. NSArray *items=[dict valueForKey:@"frames"];
  28. int itemcount = [items count];//圖片數目即動畫長度
  29. NSMutableArray *AnimFrames = [NSMutableArray array];
  30. //初始化用於存放幀的數組
  31. for (int i = 1; i <= itemcount; i++) {
  32. NSString *path = [NSString stringWithFormat:@"%@_%04d.png",animPlist,i];
  33. //組成動畫圖片的命名規則是:XXX_0001.png、XXX_0001.png...
  34. [AnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:path]];
  35. //根據key(圖片名)在內存池中查找Frame並存入幀數組中
  36. }
  37. return [CCAnimation animationWithFrames:AnimFrames delay:delay];//根據幀數組生成動畫
  38. }
  39. //-------------------------------------------------------------------------------------------分割線
  40. //精靈換圖函數
  41. -(void)changeImageBtn:(CCSprite *)changesprite imagePath:(NSString *)imagePath{
  42. CCSpriteFrame* hpframe = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:imagePath];
  43. //根據key(圖片名)在內存池中查找Frame並返回
  44. [changesprite setDisplayFrame:hpframe];//精靈換圖
  45. }
  46. //-------------------------------------------------------------------------------------------分割線
  47. //CCSpriteBatchNode,它的作用是優化精靈,提高精靈的繪制效率,精靈數量越多,效果越明顯。它的工作原理是:將所有該對象的子節點(只能是精靈)用openGL的渲染方法一次性繪制,這樣可以省去多次open-close的時間,_作為CCSpriteBatchNode對象子節點的精靈不能用自己的Texture繪制,而是用CCSpriteBatchNode對象的Texture統一繪制,精靈只是提供坐標、旋轉角度等信息,這就是它只需要open-close一次的原因,但也正因為如此,導致批處理節點的所有子節點都必須和它用同一套Texture,即CCSpriteBatchNode對象繪制出的圖形都是一樣的,這點需要格外注意。CCSpriteBatchNode的用法和普通精靈沒什麼兩樣,都可以設置Texture,也都是用Texture來繪制,只要通過addChild方法成為其子節點的精靈,都會得到它的優化,但是CCSpriteBatchNode只能添加精靈為子節點。
  48. CCSpriteBatchNode* batch=[CCSpriteBatchNode batchNodeWithFile:@"bullet.pvr.ccz"];
  49. [self addChild:batch];
  50. //CCSpriteBatchNode加到層中
  51. for(int i=0;i<100;i++){
  52. CCSprite* sprite=[CCSprite spriteWithFile:@"bullet.png"];
  53. [batch addChild:bullet];
  54. //添加子節點,子節點必須是精靈,且精靈的Texture和批處理節點相同
  55. }
Copyright © Linux教程網 All Rights Reserved