CREATING A SOUND OBJECT


To control sounds dynamically, you must use Sound objects. One of the most important things to realize about Sound objects is that you associate each one with a particular timeline in your movie at the time you create it. Thus, to dynamically control sound on the root timeline, you would need to create a Sound object and associate it with the root timeline. Or 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. Since 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. What this means is that 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; it looks like this:

 soundObjectName = new Sound (Target); 

Let's break it down: soundObjectName denotes the name of your new Sound object. You can assign any name you wish; just make sure it describes the sounds it controls and that you follow the same rules for naming your Sound object as you would for naming variables. This means 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 this Sound object will be associated with.

Once you've created a timeline-associated Sound object, you control that timeline's sound (for example, volume and panning) by referencing the name of the Sound object in your scripts, 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, you would use the following syntax:

 mySound = new Sound ("_root.myMovieClip"); 

graphics/16fig02.gif

Once you've created a Sound object associated with the movie clip instance myMovieClip, you would use the setVolume() method 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 Lesson16/Assets folder.

    This file contains two layers, Background and Ball. The Background layer contains the background graphics; the Ball layer contains the basketball graphic. This movie clip instance appropriately named basketball will eventually contain all of our project's interactivity.

  2. Double click the basketball 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 will play at the same time the bouncing ball appears to hit the floor.

    graphics/16fig03.gif

    Because there are no stop() actions on the movie clip's timeline, 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 movie clip instance. This will allow us to control the volume and panning of the bounce sound as the user drags the ball around the court.

  4. Select the basketball movie clip instance, open the Actions panel, and then add the following script:

     onClipEvent (load) {    bounce = new Sound (this);  } 

    Because it's attached to the basketball movie clip instance, this script is triggered when that movie clip instance loads (that is, when it first appears in the scene). This script's only function is to create a new Sound object named bounce that's associated with the this (basketball) timeline. Since the "bouncing" sound is part of this timeline, by controlling the bounce Sound object we'll be able to dynamically control the volume and panning of that sound.

    TIP

    Creating Sound objects using the onClipEvent(load) event handler is an efficient way of coding. It allows you to create the Sound object only after the movie clip it controls loads into the scene which makes managing and reading your code much easier.

  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 thing to realize is that as soon as the ball movie clip instance appears in the scene (which is almost instantly), a Sound object is created. The bounce won't sound different until we instruct our new Sound object to do something.

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

    You may want to keep this file open because we'll build on it as we progress through this lesson.



Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161

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