Pages

Advertisement

Friday, August 10, 2007

Beat Box

 

Figure shows a simple beat box application. To focus on the sound portion of the ActionScript, I have used the CheckBox and PushButton components.

Figure 22.1. A simple beat box application that allows the user to play with music.

graphics/22fig01.gif

  1. Open the movie 22beatboxnoscripts.fla.

  2. There are three columns of buttons. The first column is for the drum sounds. Select the first drum button and see that its Click Handler is set to buttonSound. In fact, each CheckBox and PushButton component has the Click Handler buttonSound.

  3. Where they differ, however, is their names. The drum check boxes are named drumSwitch1, drumSwitch2, and drumSwitch3. The three bass buttons are named bassSwitch1, bassSwitch2, and bassSwitch3. The three push buttons are named hitSwitch1, hitSwitch2, and hitSwitch3.

  4. This entire movie works with only two functions. The first is an advanced version of the playSound function from earlier in the hour.

    It uses four parameters. The first is a true or false value that determines whether the sound is supposed to start or stop. The second is the name of the variable to use as the sound object. The third is the name of the sound from the Library. The last parameter is whether the sound is supposed to loop.

    function playSound(startOrStop,varName,soundName,loop) {
    if (startOrStop) {
    this[varName] = new Sound();
    this[varName].attachSound(soundName);
    if (loop) {
    this[varName].start(0,9999);
    } else {
    this[varName].start(0,1);
    }
    } else {
    this[varName].stop(soundName);
    }
    }


  5. Each of the nine components in the movie calls soundButton. This calls playSound in one of nine different ways, depending on the component clicked. It uses getValue() in many cases to determine whether the check box has been clicked on or off.

    function soundButton(button) {
    if (button == bassSwitch1) {
    playSound(button.getValue(),"bassSound1","bassLoop1",true);
    } else if (button == bassSwitch2) {
    playSound(button.getValue(),"bassSound2","bassLoop2",true);
    } else if (button == bassSwitch3) {
    playSound(button.getValue(),"bassSound3","bassLoop3",true);
    } else if (button == hitSwitch1) {
    playSound(true,"hitSound1","hit1",false);
    } else if (button == hitSwitch2) {
    playSound(true,"hitSound2","hit2",false);
    } else if (button == hitSwitch3) {
    playSound(true,"hitSound3","hit3",false);
    } else if (button == drumSwitch1) {
    playSound(button.getValue(),"drumSound1","drumLoop1",true);
    } else if (button == drumSwitch2) {
    playSound(button.getValue(),"drumSound2","drumLoop2",true);
    } else if (button == drumSwitch3) {
    playSound(button.getValue(),"drumSound3","drumLoop3",true);
    }
    }

No comments:

Post a Comment