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

How to Control Independent Sound Objects Simultaneously

As stated earlier, wherever you attach the sound object, it becomes a child of that movie clip. Any call to a method of a sound object, such as setVolume, will affect all sound objects that are the child of that particular timeline. Therefore, to affect multiple sound objects independently, each sound object must be associated with its own unique movie clip. This can be an empty movie clip or one that is being used for something else. The result will be to group your sound objects by movie clip. It's very important to understand that this grouping is achieved in the way the sound object is defined - not by where it is defined.

This is a common trouble spot when first starting to learn to use sound objects. For example, you may have figured out how to fade the volume for a sound object, but you find that it is also fading the volume for all other sound objects, too. The reason for this can usually be found in how the sound object was defined.

The following is an example of how to assign a sound object as the child of a particular movie clip when the sound object is initially defined:

  1. Define the sound object
      firstSound = new Sound(firstSoundMc);

    In the above example, "firstSoundMc" is the instance name of a movie clip. Also notice that there are no quotes around the movie clip instance name.
  2. Create an empty movie clip and drag it out onto the _root stage. Give the movie clip the instance name of "firstSoundMc" (of course, you can name the move clip whatever you want. For this example, it will be "firstSoundMc").

It is not necessary for the "firstSoundMc" movie clip to contain the ActionScript that starts the sound object. The "firstSoundMc" movie clip can remain empty. The sound object "firstSound" then becomes a child of "firstSoundMc". The only purpose of firstSoundMc is to serve as a container or association for the sound object.

It is not necessary to create an associated movie clip for sound objects that only need to be started. It is needed when advanced methods such as setVolume and setPan are required for multiple independent sound objects, or when the need arises to stop one sound object while others continue.

Once this is done, the methods of "firstSound" can be manipulated independent of the other sound objects. Ways by which the sound object's methods are called do not change. In other words, once you define the sound object as being attached to a particular movie clip, you never again need to reference that movie clip to control the sound object. Since, in our example, the sound object was defined on the _root level, calls to methods of the sound object would still only need to reference the _root path.

For example, if you wanted to pan firstSound completely into the left speaker channel, the following would be used:


Note the differences in the ways the following sound objects are defined:

  firstSound = new Sound(firstSoundMc);
secondSound = new Sound();

//Then, hypothetically, on a button in the movie some place

on (press) {_root.firstSound.setPan(-100)}

//...And on a different button

on (press) {_root.secondSound.setPan(-100)}

Fig. 05: Example of effects of using and not using an associated movie clip with the sound object

In the above example, if you panned firstSound, only the sounds associated with the movie clip "firstSoundMc" would pan to the left. If you panned secondSound with the above configuration, all sounds, regardless of where they were defined or to which movie clip they were assigned, would pan to the left.

In Fig. 05 (shown to the left), two pan sliders are configured as defined above. Experiment with the sounds. Start both sounds with the green buttons. Next, pan "firstSound" left and right and see how it only effects firstSound. Then, and with both sounds playing, pan secondSound. Notice that it affects both sounds. Be sure to experiment with secondSound after firstSound.

There is another way to associate sounds with specific movie clip instances. Define the sound object as before, except instead of indicating the name of a movie clip instance, use the keyword word "this" as follows:

thirdSound = new Sound(this);

Fig. 05b: Example of using the keyword "this" to associate a sound object with the movie clips in which its being defined

If the sound object is defined using the keyword "this", calls to the sound objects, such as setVolume() and stop(), will only affect the timeline (the movie clip) where the sound object is defined. Experiment with the Flash examples in Fig05b. Notice that the sound object, thirdSound, which is defined with the keyword "this", reacts identically to the sound object "firstSound", which was associated to a specific movie clip when it was defined on the _root timeline.

Using the keyword "this" has advantages and disadvantages. One advantage is that, if the SWF file containing the sound object will be loaded into another movie using loadMovie, there is no risk of losing its association with the movie clip it was associated with when it was originally defined. The other advantage is that it is easier to configure (fewer steps). The disadvantage is that the path to the sound object must be used when calling it into action. If there are a lot of sound objects scattered about in numerous movie clips, it can become a tangled web.

» 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.