First time here? First time here? Newsletter Signup Newsletter
651326 Members and growing! | Flash Jobs
Search tutorials
Author: Kenny Bellew | Website: http://www.cowfly.com

How to Control Independent Sound Objects Simultaneously

As stated earlier, wherever you attach the sound object, it becomes a child of that movie clip. Think of the movie clip as a container for the sound. 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 is 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:

Define the sound object

firstSound = new Sound(firstSoundMc);
firstSound.attachSound("firstSound01");

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 in the ActionScript.

Create an empty movie clip and drag it out onto the _root stage (or even off 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:

_root.firstSound.setPan(-100);

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

//
firstSound = new Sound(firstSoundMc);
firstSound.attachSound("firstSound01");
//
secondSound = new Sound();
secondSound.attachSound("secondSound01");
//

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.

It is natural to think that firstSound, which is associated to firstSoundMc by definition (firstSound = new Sound(firstSoundMc)) would be protected from changes to secondSound. However, it is not. The benefit of this is that you can make a global sound control that will will override individual settings.

In Fig. 05, 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);
  thirdSound.attachSound("thirdSound01")

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. This allows you to export sound objects as SWF's, import them into a different movie clip and still have control of the sound object. How to do this will be discussed in the section: How to Preload Sound Objects that use the attachSound Method.

» Level Intermediate

Added: 2004-10-12
Rating: 8 Votes: 31
(10 being the highest)
» Author
Kenny Bellew is a technical writer and freelance flash programmer who specializes in Flash audio. He lives in Minneapolis, MN.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: doc_chin7
» Description:

The flash's horizontal road drive was done in CS4 and was upgraded to CS6. The car responds to right/ left key and flash will auto-generate the movement of roads from left-right or right-left based on random number. Have fun driving in this virtual environment and avoid other cars... :) If you are creative, a road driving game can be created from it.

Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
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: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.