Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Digital Audio, Video > Adobe Flash

  • Print
  • + Share This
From the author of

Scripting the Music Boxes

You can now focus on adding some interface elements that will enable the user to interact with this sound toy and mix the sounds. For this project, you will build a playhead, a PLAY/STOP button, and a Volume Control slider. Before you start scripting each one of those elements, however, you need to add some actions to the boxMC movie clip that will enable the user to preview the sounds and adjust the transparency (_alpha) of each box in the grid accordingly.

  1. Double-click on the boxMC movie clip in the Library to go into Edit mode. The boxMC has been assigned the instance name boxLight.

  2. Figure 9Figure 9

  3. Go to the script layer and click on Frame 1.

  4. Insert this script:

  5. Listing 04

    var activated:Boolean = false;
    boxLight._alpha = 0;

    This script sets the variable activated to false. and the _alpha property of the boxLight movie clip to 0 (full transparency). The variable activated is defined as a Boolean type.

  6. Add the following code after the last statement of the previous script:

  7. Listing 05

    onRelease = function () {
          activated = true;
          boxLight._alpha = DEFAULTBOXALPHA;
          if(_parent.playButton.isPlaying == false) {
       } else {
          activated = false;
          boxLight._alpha = 0;

    The onRelease() function controls the boxMC movie clip both when it's activated (user clicks on it) and when it's not (activated = false).

    When the user clicks on a box, then the _alpha of the boxLight movie clip is set to DEFAULTBOXALPHA (which has a value of 30) and the sound starts playing.

    If activated is true, then sound.stop("sound"+soundNum); allows the user to click on a box anytime during the preview and stop the sound associated with it.

    In Step 5 you finalize the code for the boxMC by scripting the last part, which is the _alpha fade.

  8. After the following three lines

  9. boxLight._alpha = 0;

    type this code:

    Listing 06

    onEnterFrame = function() {
       if(activated == true && boxLight._alpha > DEFAULTBOXALPHA) {

    The onEnterFrame() function fades the alpha value of the boxes if two conditions are met:

    activated == true AND boxLight._alpha > DEFAULTBOXALPHA

  10. Save your work.

  • + Share This
  • 🔖 Save To Your Account