2011-05-22 123 views
0

我有一些星星閃爍的星雲,圖像太大而無法放在一張雪碧紙上,所以我不得不將它們分散到兩張。我一直在尋找如何使用來自兩個精靈表單的圖像創建單個動畫,但沒有運氣。使用cocos2D如何使用兩個精靈表中的精靈創建動畫?

這是我試過的,它符合要求,但是當動畫到達圖像從第二個精靈表開始的位置時會崩潰。

 //=======================================================================================// 
    //Load Stars 

    frameCache = [CCSpriteFrameCache sharedSpriteFrameCache]; 
    [frameCache addSpriteFramesWithFile:@"starsOne_01.plist"]; 
    [frameCache addSpriteFramesWithFile:@"starsOne_02.plist"]; 


    starSpriteNodeOne_01 = [CCSpriteBatchNode batchNodeWithFile:@"starsOne_01.png"]; 
    [self addChild:starSpriteNodeOne_01 z:-2]; 
    starSpriteNodeOne_02 = [CCSpriteBatchNode batchNodeWithFile:@"starsOne_02.png"]; 
    [self addChild:starSpriteNodeOne_02 z:-2]; 



    //Load frames 
    starOneFrames_01 = [NSMutableArray array]; 
    for(int i = 1; i <= 12; ++i) { 
    [starOneFrames_01 addObject: 
    [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
    [NSString stringWithFormat:@"starsOne_%d.png", i]]]; 
    } 

    starOneAnim_01 = [CCAnimation animationWithFrames:starOneAnim_01 delay:0.5f]; 
    [[CCAnimationCache sharedAnimationCache] addAnimation:starOneAnim_01 name:@"starOneAnim_01"]; 


    //=======================================================================================//  
    //Add stars to the scene 

    starsOne = [CCSprite spriteWithSpriteFrameName:@"starsOne_1.png"]; 
    starsOne.anchorPoint = ccp(0, 0.5); 
    starsOne.position = ccp(0, size.height/2); 
    [starSpriteNodeOne_01 addChild:starsOne]; 

    //animate 
    starOneAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:starOneAnim_01 restoreOriginalFrame:YES]]; 
    [starsOne runAction:starOneAction]; 

任何想法,我哪裏錯了?提前致謝。

嗨@JorisMans, 我試過你的方法,它似乎一直工作,直到它試圖將動畫添加到第二個序列。我試着以相反的順序運行這些方法(所以starAnimation_02首先),它仍然給出了相同的結果。第一個方法正常運行,然後第二個要調用的方法運行時崩潰。以下是我的兩種方法,我已將CCLOG保存在其中一箇中,以便了解其崩潰的位置。程序在崩潰之前達到REACHED3 CCLOG。

-(void)starAnimation_01 
{ 

[starsOne_01 setVisible:YES]; 
[starsOne_02 setVisible:NO]; 
CCCallFunc* customCall = [CCCallFunc actionWithTarget:self selector:@selector(starAnimation_02)]; 
CCSequence* actionSeq = [CCSequence actions:[CCAnimate actionWithAnimation:starOneAnim_01 restoreOriginalFrame:NO],customCall,nil]; 

[starsOne_01 runAction:actionSeq]; 

} 

-(void)starAnimation_02 
{ 

[starsOne_02 setVisible:YES]; 
CCLOG(@"===================REACHED========================"); 
[starsOne_01 setVisible:NO]; 
CCLOG(@"===================REACHED2========================"); 
CCCallFunc* customCall2 = [CCCallFunc actionWithTarget:self selector:@selector(starAnimation_01)]; 
CCLOG(@"===================REACHED3========================"); 
CCSequence* actionSeq2 = [CCSequence actions:[CCAnimate actionWithAnimation:starOneAnim_02 restoreOriginalFrame:NO],customCall2,nil]; 
CCLOG(@"===================REACHED4========================"); 
[starsOne_02 runAction:actionSeq2]; 
} 

回答

0

每個節點都有自己的spritesheet。您無法將spritesheet圖像從一個節點應用到另一個節點。

一個可能的解決方案(對不起,不給代碼,但也許你自己看着辦吧):

  1. 創建2個CCAnimations,每個spritesheet
  2. 添加第一個星點和第二星級節點作爲「自我」
  3. 的的childNodes隱藏第二星點
  4. 應用的操作順序,你玩 動畫您 第一spritesheet的精靈的 第一球星節點,無重複, 在動畫的最後,你 做一個定製的回調
  5. 在您的自定義回調你隱藏 第一球星節點,取消隱藏第二 一個
  6. 應用的動作順序的 第二個節點你玩的 動畫您 第二spritesheet的精靈,沒有重複, 在動畫的最後,你 做一個定製的回調
  7. 在您的自定義回調你隱藏 第二顆星節點,取消隱藏第一 一個
  8. 轉到步驟4

希望這有助於你。

+0

請問您可以舉一個例子說明如何將一個自定義回調「附加」回到動畫結尾? – 2011-05-22 14:25:23

+0

CCCallFunc * customCall = [CCCallFunc actionWithTarget:self selector:@selector(myMethod)]; CCSequence動作:[CCAnimate actionWithAnimation:currentAnimation restoreOriginalFrame:NO],customCall,nil]; – 2011-05-22 14:26:40

+0

我一直在嘗試實施您的解決方案,但遇到問題,請參閱上面我的問題的編輯。請如果你能幫忙? – 2011-05-22 20:11:15