Setting the Scene
  1. Create a new Flash document that's 500 pixels wide and 400 pixels high. Set the frame rate to 24 fps (Modify > Document?).
  2. Rename the default layer Actions, and create two new folders within the Library Panel: Movie Clips and Sound.
  3. Create a new movie clip symbol in the Movie Clips folder with arbitrary content. The example in the code archive uses a static text area in which the text "Sound Effect" appears.
  4. Drag an instance of the movie clip to the stage and name it clipper.
  5. Select File > Import to Library? and choose reverb.mp3 from the code archive, or import a sound file of your choice.
  6. Select the sound clip from the Library Panel, then right-click and select Linkage?. Check the Export for ActionScript checkbox, and enter reverb as the identifier.
  7. Adding the ActionScript

    We're done setting the scene. It's time to add the code to bring it all together.

  8. Select the first frame of the Actions layer and add the following code within the Actions Panel. As this is so similar to the previous example, I've highlighted the differences in bold:
  9. Example 5.3. pancontrol.fla Actions : 1

    MovieClip.prototype.WavePan = function (minValue, maxValue, period)
    {
            this.period = period;
            this.minValue = minValue;
            this.maxValue = maxValue;
            this.count = 0;
            this.reverb = new Sound ();
            this.reverb.attachSound ("reverb");
            this.reverb.start (0, 999);
            this.onEnterFrame = function ()
            {
                    var value = (1 + Math.cos (this.count++ * 2 * Math.PI / this.period)) / 2;
                    this._x = this.minValue + value * Math.abs (this.maxValue - this.minValue);
                    this.reverb.setPan (-100 + value * 200);
            };
    };
    clipper.WavePan (50, Stage.width - 100, 48);

    We give the method a new name (WavePan), we've, set the horizontal position (instead of the vertical scale) of the movie clip to oscillate between the maximum and minimum values, and have the sound pan between the left and right speakers.

    In our call to WavePan, we pass minimum and maximum values to ensure that the movie clip covers most of the stage:

    clipper.WavePan (50, Stage.width - 100, 48);

    All that remains is to test-drive the effect!

  10. Save your Flash document and preview your work.

Move your speakers apart or put your headphones on, so you can enjoy this effect to the full.

This simple effect can serve multiple purposes within your projects. It can really bring your creations to life, adding pizzazz to animations, navigation systems, and more!

Mini Sound Player

It's time to stretch our legs! We've covered the fundamentals of sound control. Now, let's move into the development of a fully functional sound playback device. In this example, we'll create a random sound player that provides visual feedback about various properties of the random sound clips it plays, as shown in Figure 5.1.

1366_ch5001
Figure 5.1. Create a mini sound player.

To jump forward and edit this effect, locate miniplayer.fla in the code archive.

Setting the Scene
  1. Create a new Flash document that's 450 pixels wide and 100 pixels high. Set the frame rate to 24 fps (Modify > Document?).
  2. Create three new layers, naming the top layer Actions, the middle Elements, and the bottom Background.
  3. Select the Background layer and create a background element that will surround the controls, as shown in Figure 5.1. Lock the Background layer.
  4. Create four buttons (as symbols in the Library) that visually reflect the following functions: Play, Stop, Increase Volume, and Decrease Volume. Refer to miniplayer.fla in the code archive or Figure 5.1 for examples.
  5. Drag instances of the buttons into the first frame of the Elements layer, naming them PlayClip, StopClip, VolumeUp, and VolumeDown. Arrange the buttons so they sit within the bounds of the background you created in step 3.
  6. Add a static text box beneath each of the buttons to identify the tasks they perform. In the example file, I labeled them play, stop, volume + and volume -, respectively.
  7. Locate the following files in the code archive and import them into your library (File > Import to Library?): choir1.mp3, choir2.mp3, choir3.mp3, choir4.mp3, and choir5.mp3.
  8. Select each sound clip within the Library Panel, then right-click and select Linkage?. Check the Export for ActionScript checkbox and accept the default identifier value (which should be choir1, choir2, etc.).
  9. As I've explained before, this allows us to access the sound clips dynamically without having to drag them onto the stage.

    We've successfully created the objects and imported the sounds we need to achieve basic functionality. All that remains is to add the ActionScript that controls the application.