2012-08-28 63 views
0

這可能是一個簡單的問題,但是我有一個問題,試圖讓一個簡單的腳步聲發揮每一個我的性格與地面接觸時間:cocos2d的腳步聽起來

可能有人好心在此我們來看一看:

// I want to play a sound when the walk is at frame 4 
[[SimpleAudioEngine SharedEngine]playEffect:@"footstep.mp3"]; 
[[email protected]"walk%04d.png"]; 

任何幫助非常感謝!

+1

'這可能是一個簡單的問題' - 簡單的問題並不總是有簡單的答案。 –

回答

0

我認爲,計算所需的延遲要簡單得多,以便在需要的時刻播放聲音。

或者您可以將動畫分成兩部分並創建動作序列。第一個動作是CCAnimate,動畫爲0-4幀,比CCCallFunc動作,它會調用方法播放聲音,然後CCAnimate動作與其餘幀。

+0

好的......謝謝Morion,第一個不是一個選項,因爲玩家會暫停,跳躍等等。只有當特定的幀接觸地面時,時間必須是。我會研究分解動畫,儘管我會比這更簡單。 – bpok

0

你可以做這樣的事情(里程可能會有所不同,這是從我的虛弱內存泄露):

// i suppose you have your animFrames in some local var animFrames 
// and your CCSprite in some other local/iVar frameOne 

float animationDuration=0.8f; 
int animationFrames=12; 
float fourthFrameTime=animationDuration*4/animationFrames; 
float animFrameDelay=animationDuration/animationFrames; 

id animateSprite=[CCAnimation animationWithFrames:animFrames delay:animFrameDelay]; 
id playFootsteps = [CCSequence actions:[CCDelayTime actionWithDuration:fourthFrameTime], 
             [CCCallFunc actionWithTarget:self 
                  selector:@selector(playFootSteps)],nil]; 
id spawn = [CCSpawn actions:animateSprite,playFootsteps,nil]; 
[frameOne runAction:spawn]; 


// and somewhere an 

-(void) playFootSteps{ 
    [[SimpleAudioEngine SharedEngine]playEffect:@"footstep.mp3"]; 
} 

如果這是在你的應用程序反覆出現的主題,我建議你創建一個薈萃聲音管理器可以通過任何對象實例的本地協議/接口調用(通常使用'singleton',堅持使用cocos2d的一般方法),並且您可以在那裏管理聲音啓動/停止/恢復的複雜性。