Tải bản đầy đủ (.pdf) (8 trang)

Tài liệu Attaching Sounds and Controlling Sound Playback doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (24.1 KB, 8 trang )


< Day Day Up >

Attaching Sounds and Controlling Sound Playback
In non-dynamic projects, sounds are placed directly on and controlled from the timeline.
If you want a sound to play in your movie, you must drag it from the library onto the
timeline and then indicate when and how long it should play, as well as how many times
it should loop on playback. Although this may be a fine way of developing projects for
some, we're ActionScripters—we want control! That's why in this exercise we show you
how to leave those sounds in the library and call on them only when you need them.
Using other methods available to Sound objects, you'll learn how to add sounds and
control their playback on the fly.
When you create a Sound object in Flash, one of the most powerful things you can do
with it is attach a sound—in essence, pulling a sound from the library that can be played
or halted anytime you want. To do this, you must assign identifier names to all the sounds
in the library. After these sounds have identifier names, you can attach them to Sound
objects and control their playback and even their volume and panning, as we discussed
earlier in this lesson.
For example, let's assume that there's a music soundtrack in the project library with an
identifier name of rockMusic. Using the following code, you could dynamically employ
this sound in your project and control its playback:

var music:Sound = new Sound();

music.attachSound("rockMusic");

music.start(0, 5);


When executed, the first line of this script creates a new Sound object named music. The
next line attaches the rockMusic sound (in the library) to this Sound object. The third line


starts the playback of this Sound object, which in effect starts the playback of the
rockMusic soundtrack because it's attached to this Sound object. The 0 in this action
denotes how many seconds into the sound to start playback. For example, if the
rockMusic soundtrack includes a guitar solo that begins playing 20 seconds into the
soundtrack, setting this value to 20 would cause the sound to begin playback at the guitar
solo rather than at the sound's beginning. The second value in this action, which we've set
to 5, denotes how many times to loop the sound's playback. In this case, our soundtrack
will play five times before stopping.
TIP
You can set all of these values using variables or expressions—opening a world of
possibilities.

In the following exercise, we show you how to attach a random sound from the library to
a Sound object, and trigger its playback whenever the mouse button is pressed. We'll also
set up our script so that pressing any key halts the sound's playback.
1. Open basketball5.fla.
Continue using the file you were working with at the end of the preceding
exercise.
The Ball Score and Score Fields layers of our project contain assets we'll use in
this exercise. The Ball Score layer contains a movie clip instance named score_mc
above the basketball goal. This instance contains two frames labeled Empty and
Score. At the Empty frame label, the instance is, well, empty. This is its state when
it first appears in the project. At the Score label is a short animation used to
simulate the basketball going through the net, as if a score has been made. A script
we'll be adding shortly will play this animation in various circumstances.
The Score Fields layer contains two text fields named indiana_txt and
northCarolina_txt. These fields will be used to display updated scores under
various circumstances. (This will become clear shortly.)
Before we begin scripting, we need to look at some of the assets in the library.
2. Choose Window > Library to open the Library panel.

The library contains a folder called Dynamic Sounds where you'll find four sounds
that have been imported into this project. These sounds exist only within the
library; they have not been placed on our project's timeline yet.

3. Click on Sound 0 to select it. From the Option menu in the library, choose
Linkage.
The Linkage Properties dialog box appears. This is where you assign an identifier
name to the sound.
4. Choose Export for ActionScript from the Linkage options, and give this Sound an
identifier name of Sound0.

The reason we've used an identifier name ending in a number is that our script
generates a random number between 0 and 2: when 0 is generated, Sound0 plays;
when 1 is generated, Sound1 plays; when 2 is generated, Sound2 plays. The
number at the end of the identifier name is therefore crucial.
After you've assigned an identifier name to the sound, click OK.
5. Repeat steps 3 and 4 for Sound 1 and Sound 2 in the library. Give Sound 1 an
identifier name of Sound1 and Sound 2 an identifier name of Sound2.
NOTE
Library item names can contain space; identifier names cannot. When assigning
identifier names, follow the naming rules that apply to variables.
At this point, you have given three of the sounds in the Dynamic Sounds folder
identifier names of Sound0, Sound1, and Sound2. The other sound, named
Nothing but Net, has already been given an identifier of Net. This sound will be
used in a moment to simulate the sound that a basketball net makes when a ball
swishes through it.
6. With the Actions panel open, select Frame 1 of the Actions layer. After the line of
script that creates the bounce Sound object, insert the following line of script:
7.
8. var dynaSounds:Sound = new Sound();

9.

This step creates a new Sound object called dynaSounds. This Sound object will
eventually be used to randomly play one of the sounds in the library (Sound0,
Sound1, or Sound2).
Notice that when we created this Sound object, we didn't associate it with a
timeline. This means that our new Sound object will be associated with the entire
project—a "'universal" Sound object, so to speak.
7. Add the following line after the script added in Step 6:
8.
9. var netSound:Sound = new Sound ();
10.


This line creates another Sound object named netSound that will be used to play
the sound in the library with an identifier of Net.
8. Add the following lines at the end of the current script:
9.
10. this.onMouseDown = function() {
11.
12. var randomSound = random(3);
13.
14. dynaSounds.attachSound("Sound" + randomSound);
15.
16. dynaSounds.start(0, 1);
17.
18. }
19.

This onMouseDown event handler causes these lines of script to be triggered

whenever the mouse is clicked anywhere on the stage. The expression random(3);
generates one of three values between 0 and 2 and assigns this value to the
randomSound variable. The next line attaches a sound from the library to the
dynaSounds Sound object, based on the current value of randomSound. If the
current value of randomSound is 2, this would be the same as writing the
following line of script:

×