So guys, im going to insert a bunch of sound effect on my game, also i added some kind of button that could control the sounds volume. Is it possible that i could just create some lines of code that could just control the master volume of the game?
import greenfoot.*;
public abstract class Sound extends Greenfoot
{
private static GreenfootSound bgMusic;
private static int volume = 50;
public static int getVolume()
{
return volume;
}
public static void setVolume(int vol)
{
volume = vol;
if (volume > 100) volume = 100;
if (volume < 0) volume = 0;
if (bgMusic != null && bgMusic.isPlaying()) bgMusic.setVolume(volume);
}
public static void playSound(String filename)
{
GreenfootSound sound = new GreenfootSound(filename);
sound.setVolume(volume);
sound.play();
}
public static void setSoundVolume(GreenfootSound sound)
{
sound.setVolume(volume);
}
public static void setBackgroundMusic(String filename)
{
setBackgroundMusic(new GreenfootSound(filename));
}
public static void setBackgroundMusic(GreenfootSound bgm)
{
if (bgm == bgMusic) return;
if (bgMusic != null) bgMusic.stop();
bgMusic = bgm;
if (bgMusic != null) bgMusic.setVolume(volume);
}
public static void playBackgroundMusic(boolean play)
{
if (bgMusic != null)
{
if (play) bgMusic.playLoop(); else bgMusic.pause();
}
}
}
}