- Home
- Tutorials
- Flash
- Intermediate
- 'Cool and Practical' Sound Effects

Page 12 of 15
Adding the ActionScript
var backgroundOff = false;
var drumsOff = false;
var guitarOff = false;
var overlayOff = false;
function backgroundChange ()
{
if (backgroundCBox.selected)
{
backgroundOff = false;
vol1Control.enabled = true;
}
else
{
backgroundOff = true;
vol1Control.enabled = false;
_root.SoundHolder0.orchestra.setVolume (0);
}
}
function drumsChange ()
{
if (drumsCBox.selected)
{
drumsOff = false;
vol2Control.enabled = true;
}
else
{
drumsOff = true;
vol2Control.enabled = false;
_root.SoundHolder1.orchestra.setVolume (0);
}
}
function guitarChange ()
{
if (guitarCBox.selected)
{
guitarOff = false;
vol3Control.enabled = true;
}
else
{
guitarOff = true;
vol3Control.enabled = false;
_root.SoundHolder2.orchestra.setVolume (0);
}
}
function overlayChange ()
{
if (overlayCBox.selected)
{
overlayOff = false;
vol4Control.enabled = true;
}
else
{
overlayOff = true;
vol4Control.enabled = false;
_root.SoundHolder3.orchestra.setVolume (0);
}
}
function setCustomVolume ()
{
if (!backgroundOff)
{
SoundHolder0.orchestra.setVolume (vol1Control.value);
}
if (!drumsOff)
{
SoundHolder1.orchestra.setVolume (vol2Control.value);
}
if (!guitarOff)
{
SoundHolder2.orchestra.setVolume (vol3Control.value);
}
if (!overlayOff)
{
SoundHolder3.orchestra.setVolume (vol4Control.value);
}
}
var volumeListener = new Object ();
volumeListener.change = function ()
{
setCustomVolume ();
};
var cboxListener = new Object ();
cboxListener.click = function ()
{
backgroundChange ();
drumsChange ();
guitarChange ();
overlayChange ();
setCustomVolume ();
};
vol1Control.addEventListener ("change", volumeListener);
vol2Control.addEventListener ("change", volumeListener);
vol3Control.addEventListener ("change", volumeListener);
vol4Control.addEventListener ("change", volumeListener);
backgroundCBox.addEventListener ("click", cboxListener);
drumsCBox.addEventListener ("click", cboxListener);
guitarCBox.addEventListener ("click", cboxListener);
overlayCBox.addEventListener ("click", cboxListener);
Once again, we have functions to handle the CheckBox changes (backgroundChange, drumsChange, guitarChange, and overlayChange). They all look fairly similar.
function backgroundChange ()
{
if (backgroundCBox.selected)
{
backgroundOff = false;
vol1Control.enabled = true;
}
else
{
backgroundOff = true;
vol1Control.enabled = false;
_root.SoundHolder0.orchestra.setVolume (0);
}
}
These functions detect the current status of the relevant CheckBox by analyzing its selected property. If the CheckBox is selected, we set a variable to false (in this case, the variable is backgroundOff) to indicate that the corresponding clip is not switched off. These four variables are set to false to begin with, since all four sound clips are switched on at the start of the movie. We also enable the associated volume control NumericStepper (vol1Control).
If the CheckBox is not selected, we set the variable to true and disable the corresponding NumericStepper component, indicating that the sound clip is switched off. We also set the volume of the related sound clip to zero. This switches the clip off, but allows it to keep playing silently, maintaining its position in case we decide to switch it back on later.
The setCustomVolume function sets the volume of each clip to the value specified by its associated NumericStepper control, unless the flag variable indicates that it's currently switched off.
function setCustomVolume ()
{
if (!backgroundOff)
{
SoundHolder0.orchestra.setVolume (vol1Control.value);
}
if (!drumsOff)
{
SoundHolder1.orchestra.setVolume (vol2Control.value);
}
if (!guitarOff)
{
SoundHolder2.orchestra.setVolume (vol3Control.value);
}
if (!overlayOff)
{
SoundHolder3.orchestra.setVolume (vol4Control.value);
}
}
All that's left is to ensure these functions are called at the appropriate times. A new listener Object called volumeListener offers a method called change that calls the setCustomVolume function.
var volumeListener = new Object ();
volumeListener.change = function ()
{
setCustomVolume ();
};
By calling the addEventListener method from each of the NumericStepper components, we have this object handle changes to the volume of our clips:
vol1Control.addEventListener ("change", volumeListener);
vol2Control.addEventListener ("change", volumeListener);
vol3Control.addEventListener ("change", volumeListener);
vol4Control.addEventListener ("change", volumeListener);
If any of the NumericStepper component values change, the setCustomVolume function is called to handle it.
We've also set up an event listener in a similar manner for the CheckBox components:
cboxListener = new Object ();
cboxListener.click = function ()
{
backgroundChange ();
drumsChange ();
guitarChange ();
overlayChange ();
setCustomVolume ();
};
backgroundCBox.addEventListener ("click", cboxListener);
drumsCBox.addEventListener ("click", cboxListener);
guitarCBox.addEventListener ("click", cboxListener);
overlayCBox.addEventListener ("click", cboxListener);
When any one of the checkboxes is clicked, the event handler calls the four functions we defined earlier (switching on or off the correct sound clip) before calling setCustomVolume to correctly set the volume of any clip that has just been switched on.
Don't forget ActionScript 2.0
Components distributed with Flash MX 2004 work internally using features of ActionScript 2.0. If you try to publish a movie that contains such components with Publish settings configured for ActionScript 1.0, the components will not work (they'll look like white rectangles with black borders).
New movies created in Flash MX 2004 default to ActionScript 2.0, so you're okay there. But, if you start from the finished version of the previous example provided in the code archive, you'll need to change the ActionScript version setting for the file. Simply click the background of the movie, then click the Settings? button in the Property Inspector. You can adjust the ActionScript version setting on the Flash tab.
Use the checkboxes to switch on and off some of the tracks; notice how they disable and enable the volume controls we've added. Experiment with the volume controls to set a nice balance between the tracks, and see what masterpieces you can come up with!
You can build on this example to modify any number of sound object properties. Spend a little time with it and see what you can achieve.
