2014-06-19 85 views
1

我正在開發一個使用eclipse和libgdx的簡單遊戲。 目前,我正在使用'音樂'而不是'Sound'來播放我的遊戲的音效。 我做了一個按鈕,用於靜音所有的聲音fx,但是當涉及到「聲音」而不是音樂時出現問題。在libgdx中靜音'Sound'而不是'Music'

這裏是我當前的代碼:

public static Music jump; 

public static void load() { 
jump = Gdx.audio.newMusic(Gdx.files.internal("data/jump.wav")); 
} 

public static void muteFX() { 
lesgo.setVolume(0); 

public static void normalizeFX() { 
jump.setVolume(1f); 


//'muteFX' and 'normalizeFX' to be called on different class 

我想將其更改爲「聲音」(的原因我想這是對快速點擊的響應,),所以它可能是這樣的:

public static Sound jump; 

public static void load() { 
jump = Gdx.audio.newSound(Gdx.files.internal("data/jump.wav")); 
} 

/* here comes my problem, didn't know how to set mute and normal 
volume for the jump sound. I know there is also a set volume method 
to 'Sound' but really confused on the terms (long soundID, float volume) 
Can someone make this clear to me on how to implement the long and soundID? 
*/ 

我真的很新來libgdx,以及在Java。我研究了許多論壇,但仍然找不到更清楚的解釋。 任何幫助將不勝感激。

非常感謝! =)

回答

3

一個快速的建議是使用全局變量的聲音

public static VOLUME = 1.0f; 

聲音API允許你在一定的音量播放聲音,所以你可以做的是有所有的聲音中在需要時,您的遊戲將以全球價值發揮作用。

jump.play(VOLUME); 

這樣,你所有的開關都會做的是改變音量的浮點值。

public static void muteFX(){ 
    VOLUME = 0.0f; 
} 
public static void normalizeFX(){ 
    VOLUME = 1.0f; 
} 

由於內存限制,不要使用Music類來獲得音效效果將會是您最好的選擇。

我希望這有助於

+0

嘿,謝謝了很多回答,但是,試過這個,但我想知道爲什麼即使我運行/按下muteFX()方法,我也不能靜音。當它被調用時,它仍然使用「VOLUME = 1.0f」。我的聲音只是一個音效(持續1秒)而不是循環。 – jajuatco

+0

當你打電話給你正在傳遞VOLUME嗎? sound.play(VOLUME)?嘗試在運行時記錄VOLUME,Gdx.app.log(「APP」,「My Volume:」+ VOLUME); – compulsivestudios

+0

現在解決我的問題!我從這個答案中學到了很多東西。非常感謝!我的代碼的問題是,它不會在運行時記錄。我使用'VOLUME = 0.0f'從運行時遊戲類中返回了音量,並在靜音按鈕上聲明該音量來解決此問題。非常感謝! =) – jajuatco

1

在文檔它指出play()或環()方法成功時,ID返回。這可能不方便,具體取決於你想要達到的目標。 Basicly你可以得到的ID和類似於這樣使用它:

long id = sound.play(1.0f); // play new sound and keep handle for further manipulation 
sound.stop(id);    // stops the sound instance immediately 
sound.setPitch(id, 2);  // increases the pitch to 2x the original pitch 

當我想有一個按鈕,音量關和捲上我有一個全局變量之前,我玩的是聲音somethign類似我會檢查爲此:

在我的主要遊戲類別:

public static boolean soundEnabled; 

每當我想打的聲音。

if (MyMainClass.soundEnabled) 
sound.play(); 

如果你想要任何其他聲音的控制,那麼不可避免地你需要得到聲音的ID。

+0

感謝您對soundID和long的明確解釋。你是對的,在我的案例中使用它並不方便。使用了一個全局變量,它現在都在工作。非常感謝! – jajuatco