Attaching Sounds and Controlling Sound Playback
In non-dynamic projects, sounds are placed directly on and controlled from the timeline. If you want a sound to play in your movie, you must drag it from the library onto the timeline and then indicate when and how long it should play, as well as how many times it should loop on playback. Although this may be a fine way of developing projects for some, we're ActionScripterswe want control! That's why in this exercise we show you how to leave those sounds in the library and call on them only when you need them. Using other methods available to Sound objects, you'll learn how to add sounds and control their playback on the fly.When you create a Sound object in Flash, one of the most powerful things you can do with it is attach a soundin essence, pulling a sound from the library that can be played or halted anytime you want. To do this, you must assign identifier names to all the sounds in the library. After these sounds have identifier names, you can attach them to Sound objects and control their playback and even their volume and panning, as we discussed earlier in this lesson.For example, let's assume that there's a music soundtrack in the project library with an identifier name of rockMusic. Using the following code, you could dynamically employ this sound in your project and control its playback:
When executed, the first line of this script creates a new Sound object named music. The next line attaches the rockMusic sound (in the library) to this Sound object. The third line starts the playback of this Sound object, which in effect starts the playback of the rockMusic soundtrack because it's attached to this Sound object. The 0 in this action denotes how many seconds into the sound to start playback. For example, if the rockMusic soundtrack includes a guitar solo that begins playing 20 seconds into the soundtrack, setting this value to 20 would cause the sound to begin playback at the guitar solo rather than at the sound's beginning. The second value in this action, which we've set to 5, denotes how many times to loop the sound's playback. In this case, our soundtrack will play five times before stopping.
var music:Sound = new Sound();
music.attachSound("rockMusic");
music.start(0, 5);
TIPYou can set all of these values using variables or expressionsopening a world of possibilities.In the following exercise, we show you how to attach a random sound from the library to a Sound object, and trigger its playback whenever the mouse button is pressed. We'll also set up our script so that pressing any key halts the sound's playback.
Open basketball5.fla.Continue using the file you were working with at the end of the preceding exercise.The Ball Score and Score Fields layers of our project contain assets we'll use in this exercise. The Ball Score layer contains a movie clip instance named score_mc above the basketball goal. This instance contains two frames labeled Empty and Score. At the Empty frame label, the instance is, well, empty. This is its state when it first appears in the project. At the Score label is a short animation used to simulate the basketball going through the net, as if a score has been made. A script we'll be adding shortly will play this animation in various circumstances.The Score Fields layer contains two text fields named indiana_txt and northCarolina_txt. These fields will be used to display updated scores under various circumstances. (This will become clear shortly.)Before we begin scripting, we need to look at some of the assets in the library.Choose Window > Library to open the Library panel.The library contains a folder called Dynamic Sounds where you'll find four sounds that have been imported into this project. These sounds exist only within the library; they have not been placed on our project's timeline yet.


NOTELibrary item names can contain space; identifier names cannot. When assigning identifier names, follow the naming rules that apply to variables.At this point, you have given three of the sounds in the Dynamic Sounds folder identifier names of Sound0, Sound1, and Sound2. The other sound, named Nothing but Net, has already been given an identifier of Net. This sound will be used in a moment to simulate the sound that a basketball net makes when a ball swishes through it.With the Actions panel open, select Frame 1 of the Actions layer. After the line of script that creates the bounce Sound object, insert the following line of script:
This step creates a new Sound object called dynaSounds. This Sound object will eventually be used to randomly play one of the sounds in the library (Sound0, Sound1, or Sound2).Notice that when we created this Sound object, we didn't associate it with a timeline. This means that our new Sound object will be associated with the entire projecta "'universal" Sound object, so to speak.Add the following line after the script added in Step 6:
var dynaSounds:Sound = new Sound();
var netSound:Sound = new Sound ();

This onMouseDown event handler causes these lines of script to be triggered whenever the mouse is clicked anywhere on the stage. The expression random(3); generates one of three values between 0 and 2 and assigns this value to the randomSound variable. The next line attaches a sound from the library to the dynaSounds Sound object, based on the current value of randomSound. If the current value of randomSound is 2, this would be the same as writing the following line of script:
this.onMouseDown = function() {
var randomSound = random(3);
dynaSounds.attachSound("Sound" + randomSound);
dynaSounds.start(0, 1);
}
With each click of the mouse, a new number is generated, and the sound attached to this Sound object can change. The last line of the script plays the current sound attached to this Sound object. For the two parameters of this action, 0 causes the sound to play back from the beginning; the second parameter value of 1 causes the sound to play back once.Insert the following conditional statement within the onMouseDown event handler, just after dynaSounds.start(0, 1);:
dynaSounds.attachSound("Sound2");
if(randomSound == 0){
northCarolina_txt.text = Number(northCarolina_txt.text) + 2;
netSound.attachSound("Net");
netSound.start(0, 1);
score_mc.gotoAndPlay("Score");
}else if(randomSound == 1){
indiana_txt.text = Number(indiana_txt.text) + 2;
netSound.attachSound("Net");
netSound.start(0, 1);
score_mc.gotoAndPlay("Score");
}

The onKeyDown event handler causes this line of script to be triggered whenever a key is pressed. When this action is executed, playback of the dynaSounds Sound object is stopped, regardless of where it is in its playback. The last line causes the Key object to listen for this event.Choose Control > Test Movie to see how the movie operates.In the testing environment, clicking the mouse causes a random sound from the library to play. The sound stops if you press a key while the sound is playing.Close the testing environment to return to the authoring environment. Save the current file as basketball6.fla.This step completes the project! You should now be able to see how dynamic sound control enables you to add realism to your projectsand in the process makes them more memorable and enjoyable. You can do all kinds of things with sounds, including loading them from an external source, which we'll describe in Lesson 18, "Loading External Assets."
this.onKeyDown = function() {
dynaSounds.stop();
}
Key.addListener(this);