Sounds


Flash has the capability to export your sounds in MP3 format. The MP3 format compresses sounds to about a tenth the size of a raw WAV format file. Unfortunately, the compression process is relatively time-consuming , especially considering that it occurs every time you test your movie. Because you will probably test your movie frequently, you'll want to either disable the sounds in Publish Settings or wait to add the sounds until you are near the end of your project.

Playing Sounds Automatically

The artist provided me with a WAV file called radiotransmission.wav that I wanted the movie to play automatically every so often. First I initialized a new lastaction variable to 0 in frame 1 of the main timeline:

 // Initialize variable that activates radiotransmissions sound  lastaction=0; 

The built-in getTimer() function returns the number of milliseconds since the movie started. To trigger it continuously, I had to put the function inside an onClipEvent(enterFrame) event on a movie clip. I could have put the code on any movie clip, but I decided to use the player1target instance because it was already being used to swap the mouse icon when the mouse was over either the player1target or player2target movie clips. On the player1target movie clip, inside the onClipEvent(enterFrame), I added the following:

 // If the new game MC and bomb controller aren't attached  if (!_parent.thenewgame && !_parent.thebombcontroller) {        // Play the radiotransmissions sound if it's been more than         // 30 seconds since it last started         if ((getTimer()-_root.lastaction) > 30000) {            // Record when the sound is being started             _root.lastaction=getTimer();             radioSound = new Sound();             radioSound.attachSound("radiotransmissions");             radioSound.start();      }  } 

The first if statement checks to make sure that the Click to Play Another Game window or bomb controller are not attached. The next if statement evaluates whether the difference between the value returned by the getTimer() function and the lastaction variable is greater than 30,000. After the movie has been playing for 30 seconds, the radiotransmissions sound is attached and the lastaction variable is set to the current value returned by the getTimer() function. This causes the sound to begin every 30 seconds.

Bomb Sounds

To play the cannonblast.wav and bombairwhistle.wav sounds when the bomb animation starts, I added the following code to frame 1 of the bombcontroller movie clip, which contains the bomb movie clip:

 cannonSound = new Sound();  cannonSound.attachSound("cannonblast");  cannonSound.start();  whistleSound = new Sound();  whistleSound.attachSound("bombairwhistle");  whistleSound.start(); 

The bombairwhistle.wav sound is 3 seconds long, so it's possible for the bomb to return to the ground before the sound has finished. In the same frame, on the bomb movie clip, inside the else statement that is triggered when the bomb is less than 0, I added the following to turn off the sound:

 //Stop the whistle sound  _parent.whistleSound.stop(); 

To play the shellblast.wav sound when a tank is hit, I added the following code to frame 2 of the bombcontroller movie clip, which contains the shell blast movie clip:

 blastSound = new Sound();  blastSound.attachSound("shellblast");  blastSound.start(); 

To play the sandblast.wav sound when the bomb hits the sand, I added the following code to frame 3 of the bombcontroller movie clip, which contains the sand blast movie clip:

 sandSound = new Sound();  sandSound.attachSound("sandblast");  sandSound.start(); 

To play the watersplash.wav sound when the bomb lands in the water, I added the following code to frame 4 of the bombcontroller movie clip, which contains the water splash movie clip:

 waterSound = new Sound();  waterSound.attachSound("watersplash");  waterSound.start(); 

Tank Sounds

To play the tank.wav sound whenever the tank was moved, I added the following code inside if (_root.player1target.hitTest(_root._xmouse, _root._ymouse, true)), which is inside the onClipEvent(mouseDown) event on the player1tank symbol:

 tankSound = new Sound();  tankSound.attachSound("tank");  tankSound.start(); 

Inside the onClipEvent(enterFrame), when the motion variable is true, if (current-frame< frames ) becomes false and the tank is stopped , so I added the following code in the else statement to turn off the tank sound:

 // Turn the tank sound off  tankSound.stop(); 

Sound Toggle

Some players get tired of the sounds, so it's a good idea to give them a way to turn them off. First I initialized a new sound variable by adding the following code on frame 1 of the main timeline:

 // Turn sounds on by default  sounds=true; 

Then I created a new sound toggle movie clip by combining one frame each from two of the buttons in the Buttons Library that comes with Flash. Inside the new movie clip, I placed an invisible button over the top of the two buttons and then placed the following code onto it:

 on (press) {     // If sounds are on, turn them off      if (_root.sounds) {         gotoAndStop (2);          stopAllSounds ();          _root.sounds=false;      // Sounds are off, turn them on      } else {         gotoAndStop (1);          _root.sounds=true;      }      clickSound = new Sound();      clickSound.attachSound("click");      clickSound.start();  } 

When the user presses the button to toggle the sound, the click.wav sound is attached regardless of whether sounds are on. Then if the sound variable on the main timeline is true, the playhead is moved to the second frame, displaying the red button. The built-in stopAllSounds() function does exactly that. The clickSound.start() method plays the click sound. The sounds variable on the main timeline is set to false. If the sounds variable is false, the playhead is moved to the first frame, displaying the green button. The clickSound.start() method here plays the click sound even when the sounds were turned off. Finally, the sounds variable on the main timeline is set to true.

Then I put an if statement around the code throughout the movie that started playing each of the sounds. For example, the following code now checked the value of the sounds variable before starting the tank sound:

 if (_parent.sounds) {     tankSound = new Sound();      tankSound.attachSound("tank");      tankSound.start();  } 

Sound Performance

Because the sounds don't need to be attached twice, I added an if statement around the code throughout the movie that creates a copy of the sound object and attaches the sounds. For example, the following code first evaluates whether the clickSound object already exists before creating it and attaching the click.wav sound to it:

 if (_parent.sounds) {       if (!clickSound) {        clickSound = new Sound();         clickSound.attachSound("click");        }  } 


Inside Flash
Inside Flash MX (2nd Edition) (Inside (New Riders))
ISBN: 0735712549
EAN: 2147483647
Year: 2005
Pages: 257
Authors: Jody Keating

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net