I never thought development of web games can be that simple until I started development of my first game with Adobe Flex. I made two games, without any prior experience with Action script, in only ten months: "Fitter" and "RGB".

I found many answers to my questions on different forums and web sites, and it is time to share my knowledge with Flex community.

Friday, January 22, 2010

Playing sound effects

Sound effects in the game have a huge impact on the players experience. Playing sound effects with Action Script is simple, creating good sound effects is not. If you don't know how to make good sound effects for your game (like me), do not worry. You can find a lot quality sound effect on the internet for free or very small price.

In this chapter I will explain only the basics of how to play sound effects. I made three sounds effects and you can find them in Sounds folder in the project package for this chapter. All three files are in MP3 format, because Sound class, which will be used to play sound effects, supports only MP3 files. MP3 format is great for web games, because file size is small. Size of all three files together is only 90KB.

If you start the SnakesAdventure, embedded below, you can hear OptionSelected.mp3 played after mouse button click. OptionsIn.mp3 is played while the game options move from the left side to the centre. OptionsOut.mp3 is played while the game options move from the centre to the right side of the screen.





Complete source code, which takes care of embedding sound files and playing sound effects, is in the file Sounds.as. At the beginning of the file are three definitions of constants, one for each sound effect. Value of variable soundVolume controls volume of played sound effect, volume can range from 0.0 (silent) to 1.0 (max volume).

All three sound files are embedded the same way we embedded all images from Images folder.

Function PlaySound plays one sound effect, value of type parameter defines which sound effect will be played. On the basis of type parameter appropriate Sound object is made. We use Sound class play function to play sound effects. This function requires three parameters:
- initial position (in milliseconds) at which playback start
- the number of times a sound loops back to the startTime value before the sound channel stops playback
- SoundTransform object assigned to the sound channel

We will play all files from beginning and they won't loop. Only volume of the played sound effect will be adjusted. With SoundTransform object you can also adjust panning of played sound.

Initial value of soundVolume variable is 0.7, you can adjust this value as you will. So far, value remains unchanged during the game execution. Later, when we implement settings menu, player will be able to adjust sound volume with the slider.

private static const OPTIONSIN_SOUND:int = 1;
private static const OPTIONSOUT_SOUND:int = 2;
private static const OPTIONSELECTED_SOUND:int = 3;

private var soundVolume:Number = 0.7;

[Embed(source="Sounds/OptionsIn.mp3")] private var optionsInSnd:Class;
[Embed(source="Sounds/OptionsOut.mp3")] private var optionsOutSnd:Class;
[Embed(source="Sounds/OptionSelected.mp3")] private var optionSelectedSnd:Class;

private function PlaySound(type:int) : void
{
  var sfx:Sound = null;

  switch (type)
  {
  case OPTIONSIN_SOUND:
    sfx = new optionsInSnd() as Sound;
    break;
  case OPTIONSOUT_SOUND:
    sfx = new optionsOutSnd() as Sound;
    break;
  case OPTIONSELECTED_SOUND:
    sfx = new optionSelectedSnd() as Sound;
    break;
  }

  if (sfx != null)
    sfx.play(0,0,new SoundTransform(soundVolume));
}

We have all we need to play the sound effects. Let's use PlaySound function in Activation.as and GameMenu.as files.

In Activation.as we will call PlaySound when mouse button is pressed. We did not handle mouse down event in Activation.as so far, that is why we have to create MouseDown_Activation function and call it from MouseDown function in SnakesAdventure.as if ACTIVATION state is active.

private function MouseDown_Activation(event:MouseEvent):void
{
  if (activationState == ACTIVATION_WAIT)
    PlaySound(OPTIONSELECTED_SOUND);
}

There is a lot more new code in GameMenu.as. ChangeState_GameMenu function is new here and it is called whenever game menu state changes. Before that, we changed the state by changing variable gameMenuState and setting gameMenuTime to zero. ChangeState_GameMenu function does the same, but before that it checks if GAMEOPTIONS_IN or GAMEOPTIONS_OUT is becoming active and plays suitable sound effect.

private function ChangeState_GameMenu(newState:int):void
{
  switch(newState)
  {
  case GAMEOPTIONS_IN:
    PlaySound(OPTIONSIN_SOUND);
    break;
  case GAMEOPTIONS_OUT:
    PlaySound(OPTIONSOUT_SOUND);
    break;
  }

  gameMenuState = newState;
  gameMenuTime = 0;
}

PlaySound function call has also been put in MouseDown_GameMenu function, but sound effect is played only when player clicks inside game option area.

No comments:

Post a Comment