Creating a Sound Object


To control sounds dynamically, you must use Sound objects. One of the most important points to realize about Sound objects 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 would need to create a Sound object and associate it with the root timeline. To dynamically control sound in a movie clip instance, you would have to create a Sound object associated with that timeline. Sound objects are also used to control sounds in movies loaded into levels. Because Flash projects can contain multiple timelines, projects can contain several Sound objects, each controlling the sound in a different timeline.

NOTE

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


The syntax used to create Sound objects is quite simple:

 var soundObjectName:Sound = new Sound (Target); 

Let's break it down:

  • soundObjectName denotes the name of your new Sound object. You can assign any name you want; just make sure the name describes the sounds that it controls and that you follow the same rules for naming your Sound object 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 object.

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

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

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

 var mySound:Sound= new Sound ("myMovieClip_mc"); 

graphics/17inf02.gif

After you've created a Sound object 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:

 mySound.setVolume (50); 

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 object the first step in producing that bouncing ball.

  1. Open basketball1.fla in the Lesson17/Assets 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.

    graphics/17inf03.gif

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

    This movie clip contains three layers: Shadow, Graphic, and Sound. The Shadow and Graphic layers contain a couple of tweens to emulate the look and movement of a bouncing basketball. 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.

    graphics/17inf04.gif

    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.

  3. Choose Edit > Edit Document to return to the main timeline.

    Now it's time to create a Sound object 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.

  4. 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 object named bounce that's associated with the basketball_mc timeline. Because the bouncing sound is part of this timeline, we'll be able to dynamically control the volume and panning of that sound by controlling the bounce Sound object.

  5. 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 object is created. The bounce inside the basketball_mc instance won't sound different until we modify our new Sound object.

  6. 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 MX 2004 ActionScript(c) Training from the Source
Macromedia Flash MX 2004 ActionScript: Training from the Source
ISBN: 0321213432
EAN: 2147483647
Year: 2005
Pages: 182

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net