First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Kenny Bellew

How to Define a Sound Object

When utilizing sound objects, the sound is never dragged from the library onto stage or placed in a frame. 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 (except in the case of loading an external MP3).

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(); 

Fig. 01: Sound object defined
  1. Import one or more sound files into the Flash library by selecting "File" and then "Import to Library."

  2. Right-click (Windows) or control-click (Mac) on the sound file within the library and select "Linkage..."
  3. Select "Export for Actionscript" as shown in Fig02.
  4. Fig.02: Linkage properties for a sound file
    in the library.

  5. 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.
  6. Decide where to define the sound objects.

    If the sound objects are defined all on the same level, it simplifies affecting the sound object later. For example, if the sound is defined at the _root level and then is to be started by pressing a button within a movie clip, the code would appear as follows:

    on (press) {

    If each sound object is defined in different movie clips, the path to the sound object will need to be tracked. 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) {  
    When a sound object is attached using the attachSound method (as is being described here), the sound will load, by default, as a child of whichever movie clip in which the sound object was defined. However, a sound object can be assigned as a child of any movie clip, regardless of where it is defined. This is important because any call to a method of a sound object, such as setVolume, will affect all sound objects which are the child of the same movie clip. This will become more clear when you need to manipulate the methods of multiple sound objects. Manipulating multiple sound objects is articulated in the section: How to Control Independent Sound Objects Simultaneously.
  7. The ActionScript for defining your sound object will appear as in Fig.01.
        firstSound=new Sound(); 

    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.

» Level Intermediate

Added: 2002-08-13
Rating: 8.98 Votes: 664
» Author
Kenny Bellew is a technical writer in Minneapolis, MN. He's currently interested in becoming involved in other writing-related Flash projects.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.