Creating a Sound Instance

To control sounds dynamically, you must use Sound instances. One of the most important points to realize about Sound instances is that you associate each one with a particular Timeline in your movie at the time you create the sound. To dynamically control sound on the root Timeline, you need to create a Sound instance and associate it with the root Timeline. To dynamically control sound in a movie clip instance, you have to create a Sound instance associated with that Timeline.


Although a particular Timeline can contain several layers of sounds, it should be understood that when a Sound instance is created and associated with a particular Timeline, all sounds in that Timeline will be controlled equally using that single Sound instance. Setting the volume of that Timeline's Sound instance to 50 will relatively decrease all sounds on all layers of that Timeline by 50 percent.

The syntax used to create Sound instances is quite simple:

   var soundInstanceName:Sound = new Sound (Target);

Let's break it down:

  • soundInstanceName denotes the name of your new Sound instance. You can assign any name you want; just make sure that the name describes the sounds that it controls and that you follow the same rules for naming your Sound instance as you would for naming variables: no spaces, punctuation marks, or numbers as the first character of the name.

  • The syntax new Sound is ActionScript's way of creating a new Sound instance.

  • (Target) is where you indicate to the Timeline which target path will be associated with this Sound instance.

After you create a Timeline-associated Sound instance, you control that Timeline's sound (for example, volume and panning) by referencing in your scripts the name of the Sound instance, not the target path or instance name of the Timeline.

Let's look at a real example. To create a Sound instance to control the sound in a movie clip instance named myMovieClip_mc, you use the following syntax:

   var mySound:Sound = new Sound (myMovieClip_mc);

After you create a Sound instance associated with the movie clip instance myMovieClip_mc, you would use the setVolume() method of the Sound class to dynamically adjust the volume to 50 percent, as the following syntax shows:


As mentioned earlier, the goal of this lesson's project is to simulate the sound of a basketball bouncing around the court. In the exercise that follows, we'll create a Sound instancethe first step in producing that bouncing ball.


Open basketball1.fla in the Lesson16/Start folder.

This file contains six layers: Background, Ball, Ball Score, Score Fields, Watermark, and Actions:

  • The Background layer contains the background graphics.

  • The Ball layer contains the basketball graphic, which is a movie clip instance appropriately named basketball_mc. We'll be looking at this instance's Timeline in a moment because it plays a part in the functionality of our project.

  • The Ball Score layer contains a movie clip named score_mc (placed in front of the goal) that will be used to simulate the ball swishing into the goal. We'll be working with this instance later in the lesson.

  • The Score Fields layer contains a couple of text field instances named indiana_txt and northCarolina_txt that will eventually be used to display scores. We'll discuss these in a later exercise as well.

  • The Watermark layer contains a see-through graphic that appears on the lower-right portion of the stage, simply to give our project a realistic "television station" feel.

  • The Actions layer will contain most of the scripts for this project.


Double-click the basketball_mc movie clip instance to open its Timeline.

This movie clip contains three layers: Sound, Graphic, and Shadow. The Sound layer simply contains a "bounce" sound on Frame 5. This sound plays at the same time the bouncing ball appears to hit the floor. The Shadow and Graphic layers contain a couple of tweens to emulate the look and movement of a bouncing basketball.

Because the movie clip's Timeline doesn't include a stop() action, playback will continue to loop, giving the effect of a continuously bouncing ball.


Choose Edit > Edit Document to return to the main Timeline.

Now it's time to create a Sound instance associated with the basketball_mc movie clip instance. This object will allow us to control the volume and panning of the bounce sound as the user drags the ball around the court.


With the Actions panel open, select Frame 1 of the Actions layer and add the following script:

   var bounce:Sound = new Sound(basketball_mc);

The only function of this line of script is to create a new Sound instance named bounce that's associated with the basketball_mc Timeline. Because the bouncing sound is part of this Timeline, we can dynamically control the volume and panning of that sound by controlling the bounce Sound instance.


Choose Control > Test Movie to see the movie play.

In its current state, our project doesn't appear very dynamic. You can't drag the ball around, and the bouncing sound maintains a consistent volume and pan throughout. We'll remedy this situation as we progress through this lesson. The important point to realize is that as soon as the movie begins to play, a Sound instance is created. The bounce inside the basketball_mc instance won't sound different until we modify our new Sound instance.


Close the testing environment to return to the authoring environment. Save the current file as basketball2.fla.

We'll build on this file as we progress through this lesson.

Macromedia Flash 8 ActionScript Training from the Source
Macromedia Flash 8 ActionScript: Training from the Source
ISBN: 0321336194
EAN: 2147483647
Year: 2007
Pages: 221 © 2008-2017.
If you may any questions please contact us: