Controlling the Sound Object 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 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).
- Open the Actions Panel (F9). Carefully and precisely type
the script displayed in Figure 8 into the script pane of the panel.
Figure 8 Carefully type this script for the slider button into the script pane of the actions window.
- The first script (onClipEvent(Load)) 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
left = _x-100;
Experiment with changing this value and see what effect it has on the movement of the slider).
- The next script is the money script for this whole tutorial. It uses Flash’s SetVolume command to establish the volume of the s Sound object by correlating it with the location of the slider. The further 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 further to the right it moves.
If you experience any difficulties, review the three troubleshooting tips I outlined earlier in this article. If you are still experiencing problems, carefully review the scripts and file setup in the sample FLA file that you downloaded previously.
Figure 9 ActionScript for creating a vertical slider
That’s it. You now have a very versatile and elegant volume control. What’s more, you have learned how to create a Sound object that can be manipulated and controlled in a whole host of ways, giving you a much greater degree of control and flexibility with the audio assets of your Flash projects. For example, although it is currently set to 100 pixels in Flash, after you publish the SWF file, you can scale this volume control to whatever size you need, large or small!
For more information on controlling Flash audio with ActionScript, see the free ActionScript tutorials on my Web site.