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.
Double-click on the boxMC movie clip in the Library to go into Edit mode. The boxMC has been assigned the instance name boxLight.
Go to the script layer and click on Frame 1.
Insert this script:
Add the following code after the last statement of the previous script:
After the following three lines
Save your work.
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.
Listing 05
onRelease = function () { if(activated==false){ activated = true; boxLight._alpha = DEFAULTBOXALPHA; if(_parent.playButton.isPlaying == false) { sound.start(); } } else { sound.stop("sound"+soundNum); 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.
boxLight._alpha = 0; } }
type this code:
Listing 06
onEnterFrame = function() { if(activated == true && boxLight._alpha > DEFAULTBOXALPHA) { boxLight._alpha--; } }
The onEnterFrame() function fades the alpha value of the boxes if two conditions are met:
activated == true AND boxLight._alpha > DEFAULTBOXALPHA