1. 程式人生 > >cocos2d精靈與動畫

cocos2d精靈與動畫

總結一些精靈與動畫操作的方法,其實主要是對CCSpriteFrameCache和CCAnimationCache的理解與使用

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Jiangshi_Zoulu.plist"];
//根據一個plist檔名構建CCSpriteFrame物件並新增到記憶體池中。缺省了Texture檔名的構建方法,預設的檔名先在_plist中查詢,如果沒有則查詢和plist同名的檔案。
CCAnimation *jiangshi_zoulu=[self animationFromPlist:@"Jiangshi_Zoulu" delay:0.2];
//根據plist內容生成動畫,其實就是根據動畫圖片的名稱生成動畫,animationFromPlist函式在後面
[[CCAnimationCache sharedAnimationCache] addAnimation:jiangshi_zoulu name:@"jiangshi_zoulu"];
//向池中新增一個CCAnimation物件,索引的key為jiangshi_zoulu

//-------------------------------------------------------------------------------------------分割線
CCSprite *risex=[CCSprite spriteWithSpriteFrameName:@"risex_0001.png"];
//根據幀的名字建立一個精靈物件
CCAnimation *jiangshi=[[CCAnimationCache sharedAnimationCache] animationByName:@"jiangshi_zoulu"];
//根據key:jiangshi_zoulu從池中獲取CCAnimation物件
id action=[CCAnimate actionWithAnimation:jiangshi];
//根據CCAnimation生成動畫效果CCAnimate
[risex runAction:[CCRepeatForever actionWithAction:action]];
//讓精靈risex重複執行動畫效果

//-------------------------------------------------------------------------------------------分割線
[[CCAnimationCache sharedAnimationCache] removeAnimationByName:@"jiangshi_zoulu"];
//根據key從池中刪除CCAnimation物件
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromFile:@"Jiangshi_Zoulu.plist"];
//從記憶體池刪除plist中列出的Frame,相當於addSpriteFramesWithFile的逆向操作

//-------------------------------------------------------------------------------------------分割線
//生成動畫函式
- (CCAnimation *)animationFromPlist:(NSString *)animPlist delay:(float)delay {
	
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:animPlist ofType:@"plist"]; // 1
    NSMutableDictionary* dict = [[[NSMutableDictionary alloc] initWithContentsOfFile:plistPath] autorelease];
    //讀取plist內容到字典dict中	
    NSArray *items=[dict valueForKey:@"frames"];
    int itemcount = [items count];//圖片數目即動畫長度
    NSMutableArray *AnimFrames = [NSMutableArray array];
    //初始化用於存放幀的陣列
    for (int i = 1; i <= itemcount; i++) {
        NSString *path = [NSString stringWithFormat:@"%@_%04d.png",animPlist,i];
        //組成動畫圖片的命名規則是:XXX_0001.png、XXX_0001.png...
        [AnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:path]];
        //根據key(圖片名)在記憶體池中查詢Frame並存入幀陣列中
    }
    return [CCAnimation animationWithFrames:AnimFrames delay:delay];//根據幀陣列生成動畫
}

//-------------------------------------------------------------------------------------------分割線
//精靈換圖函式
-(void)changeImageBtn:(CCSprite *)changesprite imagePath:(NSString *)imagePath{
	CCSpriteFrame* hpframe = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:imagePath];
	//根據key(圖片名)在記憶體池中查詢Frame並返回
	[changesprite setDisplayFrame:hpframe];//精靈換圖
}

//-------------------------------------------------------------------------------------------分割線
//CCSpriteBatchNode,它的作用是優化精靈,提高精靈的繪製效率,精靈數量越多,效果越明顯。它的工作原理是:將所有該物件的子節點(只能是精靈)用openGL的渲染方法一次性繪製,這樣可以省去多次open-close的時間,_作為CCSpriteBatchNode物件子節點的精靈不能用自己的Texture繪製,而是用CCSpriteBatchNode物件的Texture統一繪製,精靈只是提供座標、旋轉角度等資訊,這就是它只需要open-close一次的原因,但也正因為如此,導致批處理節點的所有子節點都必須和它用同一套Texture,即CCSpriteBatchNode物件繪製出的圖形都是一樣的,這點需要格外注意。CCSpriteBatchNode的用法和普通精靈沒什麼兩樣,都可以設定Texture,也都是用Texture來繪製,只要通過addChild方法成為其子節點的精靈,都會得到它的優化,但是CCSpriteBatchNode只能新增精靈為子節點。

CCSpriteBatchNode* batch=[CCSpriteBatchNode batchNodeWithFile:@"bullet.pvr.ccz"];
[self addChild:batch];
//CCSpriteBatchNode加到層中
for(int i=0;i<100;i++){
	CCSprite* sprite=[CCSprite spriteWithFile:@"bullet.png"];
	[batch addChild:bullet];
	//新增子節點,子節點必須是精靈,且精靈的Texture和批處理節點相同
}