2012年2月6日 星期一

因應 cocos2d 1.0 將CCSpriteSheet 移除之後的 CCAnimation 類別的使用方式





繼續來記錄關於更新之後造成差異的修正。
因為在 「知易 Cocos2d - iPhone 開發教程 3 - ZYG003 」 的範例中,為了展示 CCAnimation 的功用,所用到的仍然是舊版的 CCSpriteSheet
因此必須加以修正才能讓範例程式能順利地運作。

原始的範例程式如下:
//  ------------------------------------------------------------------------------------------------

- (void) OnAnimation:(id) sender
{
    CCSpriteSheet *mgr = (CCSpriteSheet *)[self getChildByTag:4];

    CCAnimation *animation = [CCAnimation animationWithName:@"flight"delay:0.2f];
    for(int i=0;i<3;i++) {
        int x= i % 3;
        [animation addFrameWithTexture:mgr.texture rect: CGRectMake(x*32, 0, 31, 30) ];
}

    id action = [CCAnimate actionWithAnimation: animation];
    [sprite runAction:[CCRepeat actionWithAction:action times:10]];

}

//  ------------------------------------------------------------------------------------------------


在 google 上能找到和這個修正有關,又是中文的文章好少,還好有發現這一篇文章:
http://blog.sina.com.cn/s/blog_8c7c56230100uw7j.html cocos2d 中 CCAnimation 的使用 ( cocos2d 1.0 以上版本 )
※在此感謝一下該部落格格主 ^^"

該段修正 code 資訊類似:
//  ------------------------------------------------------------------------------------------------
- ( void ) OnAnimation: ( id ) sender
{

    CCTexture2D      * textureAnime        = [[CCTextureCache sharedTextureCache] addImage:@"***.png"];
    CCSpriteFrame    * frameSprite         = [CCSpriteFrame frameWithTexture:textureAnime rect:CGRectMake( 0, 0, textureAnime.contentSize.width, textureAnime.contentSize.height)];
    NSArray          * arrayFrame          = [[NSArray alloc] initWithObjects: frameSprite, nil];
    CCAnimation      * animationData       = [CCAnimation animationWithFrames: arrayFrame delay:0.2f];
    
    int                iX                  = 0;
    for ( int i = 0; i < 3; i++ )
    {
        iX                                  = i % 3;
        [animationData addFrameWithTexture:textureAnime rect: CGRectMake( iX*32, 0, 31, 30 )];
    }
    id                 actAnimate          = [CCAnimate actionWithAnimation:animationData];

    [flight runAction:[CCRepeat actionWithAction:actAnimate times:10]];
}

//  ------------------------------------------------------------------------------------------------

很不幸的,雖然這段 code 的修正確實是讓 CCAnimation 可以順利地運作了,只可惜執行之後會有個問題發生,
它會在動畫顯示的過程中把原始貼圖的一整張完整貼圖也順道在執行的過程中播放出來了,

找了一陣子資訊後,決定還是乖乖的用 debug 模式來查看看有沒有辦法找到問題,終於找到原因是在 animationData 當中的 frame 的數量和預期的相比多了一張,
而且就是完整的一整張貼圖被拿來當作 frame 0 了。 =..="

結果是因為初始化過程的這一段 在進行的同時就把整張貼圖也丟進去了的原因
//  ------------------------------------------------------------------------------------------------
    NSArray          * arrayFrame          = [[NSArray alloc] initWithObjects: frameSprite, nil];

//  ------------------------------------------------------------------------------------------------

基本上,因為初始化過程設定了 frameSprite 這個 CCSpriteFrame 的物件變數: frameSprite 的原因,
因此只需要把初始化過程設定的地方移除掉就可以了。

改成這樣:
//  ------------------------------------------------------------------------------------------------
    NSArray          * arrayFrame          = [[NSArray alloc] initWithObjects: nil];

//  ------------------------------------------------------------------------------------------------
因為 frameSprite 這個物件變數在此也用不上了,所以它的宣告以及初始化的過程,可以一整個移除掉了。

整段 改完之後其實就只剩這個樣子:
//  ------------------------------------------------------------------------------------------------
- ( void ) OnAnimation: ( id ) sender
{

    CCTexture2D      * textureAnime        = [[CCTextureCache sharedTextureCache] addImage:@"***.png"];
    NSArray          * arrayFrame          = [[NSArray alloc] initWithObjects: nil];
    CCAnimation      * animationData       = [CCAnimation animationWithFrames: arrayFrame delay:0.2f];
    
    int                iX                  = 0;
    for ( int i = 0; i < 3; i++ )
    {
        iX                                  = i % 3;
        [animationData addFrameWithTexture:textureAnime rect: CGRectMake( iX*32, 0, 31, 30 )];
    }
    id                 actAnimate          = [CCAnimate actionWithAnimation:animationData];

    [flight runAction:[CCRepeat actionWithAction:actAnimate times:10]];
}

//  ------------------------------------------------------------------------------------------------
//  ------------------------------------------------------------------------------------------------

※類似後記:其實網路上已經找得到透過使用 plist 方式進行貼圖檔案讀入的方式,來加速 coding 速度的做法和工具了,
  只是在想如果下次又遇到一樣的笨問題時,會不會又忘記了,所以也拿來記錄一下。















1 則留言:

  1. ※連範例程式都還沒摸完的狀況,感覺有人打算要開始開發 APP 的 fu,
    會不會太快了啊 XD 就不能讓我用這樣的模式在摸魚摸上一段時間嗎 ? .?

    回覆刪除