Controlling Sound Object Volume with ActionScript
Now that you have created and tested your Sound object, you are ready to use ActionScript to make it perform useful tasks, such as controlling volume.
This is where the slider and rail graphic interface elements come into play. You’ll next write scripts to perform two tasks: one is to constrain the movement of the slider to the confines of the rail, simulating a real sider control; the other is to correlate the position of the slider to the volume of the Sound object.
- Select the slider object on the stage by clicking it with the selection tool (this must be a button or other symbol for it to accept the script you will add next). It is imporant that the following script is added to the slider, not the rail or play button.
- Open the Actions Panel (F9). Carefully and precisely type
the script displayed in Figure 7 into the script pane of the panel.
Figure 7 Carefully type this script for the slider button into the script pane of the actions window.
- The first line of the script (onClipEvent(Load)) is triggered when the movie
clip loads(which is when the SWF file is downloaded into the browser) and sets
the location of the slider. Because both the top and bottom values
equal the current location of the slider along the y-axis, this fixes the slider
in the horizontal direction (translation: it can’t move up or down). The
following statement further limits the movement of the slider in the horizontal
direction to 100 pixels max:
left = _x-100;
If you experiment by substituting the 100 with a higher number, you will notice that the slider moves beyond the edge of the rail. A lower number prevents the slider from moving to the end of the rail.
- The next line is the money script for this volume slider. It uses Flash’s SetVolume command to establish the volume of the mysound Sound object by correlating it with the location of the slider. The farther the slider is to the right (up to a max of 100), the higher the volume.
- Now is a good time to test your script again. Test your movie (Control > Test Movie) and press the play button. Move the slider back and forth. The slider should stay on the rail, move a maximum of 100 pixels, and increase the volume the farther to the right it moves.
That’s it. You now have a working volume control. What’s more, you have practiced creating and using some important ActionScript components, including functions and objects. Specifically, you have learned how to create a custom Sound object that can be manipulated and controlled in a variety of ways.
Remember that manipulating objects and object classes is really at the heart of effectively using the Flash ActionScript language.
For more information on controlling Flash audio with ActionScript, see the free ActionScript tutorials on my web site.