Attaching a Sound


When you've created a new Sound object, you are almost ready to use ActionScript to attach and control your sounds. Before you can start down that path , however, you must first let Flash know which sounds your new Sound object (or any other Sound object you create in the movie) can use. You do that by modifying the Linkage Properties of those sounds in the Library. You must provide a name , or identifier , to each sound that you want to be able to attach and control with ActionScript.

You can modify the Linkage Properties for a sound in the Library by choosing the sound, right-clicking (or Control-clicking) it, and selecting Linkage from the pop-up menu.

When the Symbol Linkage Properties dialog box opens, just set Linkage to Export This Symbol and give it an identifier. The identifier should not contain any spaces or special characters , and it shouldn't start with a number.

The attachSound method is used to attach sounds to the timeline targeted by the Sound object that you created. The syntax for the attachSound method is this:

 mySound.attachSound("identifier"); 

This attaches the sound specified in the identifier argument to the mySound object. The sound has to be in the current movie's Library, and it has to have an identifier set in the Linkage Properties for that sound. The identifier that you set in the Linkage Properties corresponds with the identifier that you specify in the attachSound method's argument.

Note

The identifier argument must be enclosed in quotation marks (" "), unless you want to use an expression to create the name of the identifier. You learn how to use an expression to set the identifier argument later in this chapter.


The attachSound method only attaches a sound to the Sound object. It does not make the sound play. You learn how to do that later in this chapter.

Note

When you attach a sound, the entire sound has to load before the first frame in the movie's main timeline can play. Thus, you have to be careful that you don't add large sounds (more than a few kilobytes) because, if you do, even a preloader will fail to play! You learn more about this later on in this chapter.


You can attach only one sound to an instance of the Sound object at a time. If you try to attach a second sound, it replaces the first one. The following ActionScript attaches only one sound to a timeline:

 sound1 = new Sound(this);  sound1.attachSound("music1");  sound1.attachSound("music2"); 

In this case, only the second sound (music2) actually is attached to the timeline (this).

You can attach multiple sounds to a single timeline (main or movie clip), but you must remember to make a separate Sound object for each sound. The following ActionScript enables you to attach more than one sound to a single timeline:

 sound1 = new Sound(this);  sound2 = new Sound(this);  sound1.attachSound("music1");  sound2.attachSound("music2"); 

In this case, both music1 and music2 are attached to the timeline (this). All the sounds in a single timeline are controlled together.

Exercise 18.2 Attaching a Sound

Now that you have a new Sound object, you can attach a sound to it.

  1. Make sure you still have volume.fla open from the last exercise. Locate loop.mp3 in the Sounds folder in this movie's Library. Right-click or Control-click the sound and choose Linkage from the pop-up menu.

  2. In the Symbol Linkage Properties dialog box, click the Export this symbol option button, and set Identifier to theLoop. Click OK to apply these settings. (See Figure 18.3.)

    Figure 18.3. Set the Symbol Linkage Properties for the loop.

    graphics/18fig03.gif

  3. Open the Volume Controller movie clip in Symbol-Editing mode, if it's not already open. Select frame 1 of the Actions layer. In the Actions panel, add the following line to the end of the ActionScript to attach the sound:

     mySound.attachSound("theLoop"); 

Your sound still won't play at this point because you haven't explicitly told it to start. That's what you do in the next exercise. Save the file so that you can continue.



Inside Flash
Inside Flash MX (2nd Edition) (Inside (New Riders))
ISBN: 0735712549
EAN: 2147483647
Year: 2005
Pages: 257
Authors: Jody Keating

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net