Sound effects

From Gardenwiki

Jump to: navigation, search

Here is one easy way to use sound effects in a Game Gardens game.

Contents

sounds.properties file

In the same folder as your game's properties file (rsrc/i18n/), create a new file named sounds.properties . For each WAV file that you will use, make a line similar to this one:

   scissors        = media/scissors.wav

Use a code word on the left that is descriptive of the way you will use the sound, and put the location of the sound on the right.

BoardView changes

New variables

Add the following new variables, and imports needed for them:

   import com.threerings.resource.ResourceManager;
   import com.threerings.media.sound.SoundManager;
   protected ResourceManager _rmgr;
   protected SoundManager _smgr; 

Initialize the SoundManager

The following method can be added to your BoardView, then called in BoardView's constructor (or later, but before you need the sounds). _ctx is the ToyBoxContext for the BoardView.

   private void initSounds() {
       _rmgr = _ctx.getToyBoxDirector().getResourceManager();        
       _smgr = new SoundManager(_rmgr);
   }

Play the Sound

If you will be using many different sounds, you will likely want to make a playSound method. Assuming you just want one sound played, add a line like this one:

    _smgr.play(GAME_FX, PKG_PATH, text);

Either BoardView or an interface that BoardView implements such as a Codes class needs to implement the SoundCodes interface. SoundCodes has the constant GAME_FX in it.

PKG_PATH is a String that gives the location of the sounds.properties file. Normally this will be "rsrc/i18n/".

text is a String that is mapped in the sounds.properties file. That is, the String that is on the left hand side of the = in that file. If we wanted to play scissors.wav, then we could use "scissors" for text.

Optional: Allow disabling of sounds

Some players find sounds annoying. It's a good idea to add a toggle to the lobby to allow them to set the table to not play sounds. GameManager would then read this parameter and use it to set the value of a boolean field in the GameObject class. Then in your BoardView's playSound method, just add lines similar to these:

       if (!_gameobj.playSounds)
           return;     // don't play sounds
Personal tools