| How to Define a Sound Object Using the attachSound Method | Return to Index | Do you want all FLA's used for this tutorial? | Do you have a question? |
When utilizing sound objects and the attachSound method, the sound is never dragged from the library onto stage or placed in a frame. This is true whether you use the attachSound (for embedded sounds) or the loadSound method (for external sound files). The sound is defined in a frame or movie clip and an identifying name is attached to the sound while it resides in the library.
The attachSound method attaches files or grabs files from the SWF's library. In contrast, the loadSound method is used for loading files that are external to the SWF. Both methods define their objects very similar.
For this section of the tutorial, "firstSound" will refer to the name chosen for the instance of the sound object. Similarly, "firstSound01" will refer to the identifying name inserted in the linkage properties of the sound file that will be imported into the Flash library.
firstSound=new Sound();
firstSound.attachSound("firstSound01");
______________________________ |
Frankly, I always define my sound objects on the _root level. This way, you never have to remember the path to the sound object. It will always be on the _root. To follow this suggestion, define your sound objects as follows.
_root.firstSound=new Sound();
_root.firstSound.attachSound("firstSound01");
______________________________ |
Note: "new Sound()" and "attachSound" are case sensitive.
Import one or more sound files into the Flash library by selecting "File" and then "Import to Library."
Right-click (Windows) or control-click (Mac) on the sound file within the library and select "Linkage..."
Select "Export for Actionscript" as shown in Fig02.
|
Type in an identifier for the linkage ID.
The identifier, which, in this example, is firstSound01, must be unique. It should not be the same as your sound object instance name or any other identifier in your movie. It also should not start with a number or illegal character.
on (press) { |
When a call is made to the sound object, the complete path to the location where the sound object is defined will need to be spelled out in the ActionScript. For example, if the sound object is defined in a movie clip named "mc02" which is in another movie clip called "mc01", the ActionScript would appear as follows:
| on (press) { _root.mc01.mc02.firstSound.start(); } |
| _root.firstSound=new Sound(); _root.firstSound.attachSound("firstSound01"); |
In this example, the chosen sound object instance name is "firstSound". In the second line, the instance name of "firstSound" is attached to a sound file in the library by specifying that sound's linkage identifier, "firstSound01," which was created in the above step 4 and illustrated in Fig.02.
Your sound object is now defined and ready to be called into action.
Note: Never start the name of your sound object or linkage ID with a number.
|
Return to Index | Do you want all FLA's used for this tutorial? | Do you have a question? |
This document copyright © 2005, 2006 by Kenny Bellew of Cowfly.Com Design, kennybellew@hotmail.com