2012-07-19 142 views
0

在iPhone的cocos2d中,在一個場景中,如何淡出一個圖層並淡入另一個圖層?圖層的過渡效果

的想法是,我有一個屏幕:

  • 一層具有分頁控件在頂部
  • 屏幕的其餘部分填充(如切換菜單項,並選擇完成)另一個顯示當前頁面內容的圖層。

現在,一旦用戶單擊任何分頁控件,我想淡出當前頁面的內容層(但保留分頁層),並淡入下一頁的內容層。這兩個圖層都是相同的圖層,它們根據變量currentPage從plist中提取數據,所以我需要刷新圖層。

我知道在場景中,當調用replaceScene時,可以指定一個過渡效果。做到這一點,一切正常。但顯然,它也會淡化分頁控件,這看起來很愚蠢。那麼它對於圖層如何工作?

回答

0

我認爲你可以使用runAction:讓你CCLayer就做CCAction S(如CCFadeInCCFadeOut),這樣就可以達到你想要的。

你需要兩個「內容層」來分別保存當前頁面(頁面A)和下一頁面(頁面B)。在兩個內容層的淡出操作結束後,您可以清理頁面A.

1

hmmm ....使用CCLayerColor(它實現CCRGBAProtocol協議),淡入淡出將傳播到其中的任何對象。然後做這樣的事情:

-(void) buttonTouchedCallBack{ 
    id out = [CCFadeTo actionWithDuration:.35 opacity:0]; 
    id callFunc = [CCCallFunc actionWithTarget:self selector:@selector(changeContent)]; 
    id in = [CCFadeTo actionWithDuration:.35 opacity:255]; 
    id enableMenus = [CCCallFunc actionWithTarget:self selector:@selector(layerInView)]; 

    _menu.isTouchEnabled=NO; 
    [_contentLayer stopAllActions]; 
    [_contentLayer runAction:[CCSequence actions:out,callFunc,in,enableMenus,nil]]; 
} 

-(void) changeContent{ 
    // do your stuff here 
} 

-(void) layerInView{ 
    _menu.isTouchEnabled=YES; 
    // and anything else that is appropriate 
} 
+0

+1此解決方案 – mokagio 2012-07-25 16:16:58

0

我寫這將使用塊給作爲場景轉換相同淡入,淡出效果一點點功能,但是對於單個層。只需傳入要掩蓋的圖層,淡入淡出的速度,淡入的顏色以及希望在圖層隱藏時執行的塊。

-(void)fadeLayer:(CCLayer*)layer withOutDuration:(float)outTime inDuration:(float)inTime color:(ccColor3B)color withBlock: (void(^)())block 
{ 
    CGSize winSize = [[CCDirector sharedDirector] winSize]; 
    CCLayerColor *toplayer = [CCLayerColor layerWithColor:ccc4(color.r, color.g, color.b, 0) width:winSize.width height:winSize.height]; 

    [layer addChild:toplayer z:INT_MAX]; 

    [toplayer runAction: 
     [CCSequence actions: 
     [CCFadeIn actionWithDuration:outTime], 
     [CCCallBlock actionWithBlock:block], 
     [CCFadeOut actionWithDuration:inTime], 
     [CCCallBlockN actionWithBlock:^(CCNode *node) { 
     [node removeFromParentAndCleanup:YES]; 
     }], 
    nil]]; 
}