The Sound Object


The Sound object lets you use ActionScript to attach and control sounds in any time-line of your movie. You can use the Sound object to attach a sound to an instance of a movie clip or the main timeline. You then can start and stop that sound, as well as control its volume and pan. Before you can attach or control a sound, you must use the Sound object constructor to create an instance of the Sound object. The syntax for the Sound object constructor is this:

 new Sound(target); 

If you do not specify a target in the Sound object constructor, the Sound object controls all the sounds in the timeline, as well as sounds in movie clips that are on the Stage or attached with ActionScript. This actually can be quite useful, if you want to control all the sounds in your movie at once. You can specify a target in the constructor function to control the sounds of just that target.

Exercise 18.1 Creating a New Sound Object

The first step to adding and controlling your sound with ActionScript is to create a new Sound object.

  1. Open volume.fla from the Chapter_18/Assets folder on the CD. This movie has three layers (see Figure 18.1), each of which has a total of 35 frames .

    Figure 18.1. The movie contains three layers.

    graphics/18fig01.gif

    The three layers are as follows :

    • Actions. This layer has a keyframe in frame 35. That keyframe contains a Stop action.

    • Volume Controller. This layer has an empty keyframe in frame 35. You are going to add some content to that keyframe later in this chapter.

    • Background. This layer contains the Background Clip movie clip, which has an animation to provide a little visual stimulation while your sounds play. The Background Clip movie clip also has a sound in it.

  2. Open the Library for this movie. Select frame 35 of the Volume Controller layer and drag an instance of the Volume Controller movie clip onto the Stage. Use the Info panel (see Figure 18.2) to set:

    X: 275

    Y: 75

    Figure 18.2. Use the Info panel to position the movie clip in the Volume Controller layer.

    graphics/18fig02.gif

  3. Double-click the instance of the Volume Controller movie clip that you just added to open it in Symbol-Editing mode. Select frame 1 of the Actions layer and open the Actions panel. To initialize the slider control variables , add the following ActionScript to this frame:

     //Initialize the variables for the slider control  leftSide = 0;  rightSide = 550;  topSide = this._y;  bottomSide = this._y; 
  4. Now select the instance of the Button symbol that's in the Button layer. Apply the following ActionScript to this button:

     on (press) {     this.startDrag(false, leftSide, topSide, rightSide, bottomSide);  }  on (release) {     this.stopDrag();  } 

    This ActionScript creates simple drag-and-drop interactivity for the Volume Controller movie clip. The constraining rectangle for the startDrag method makes use of the leftSide, topSide, rightSide, and bottomSide variables. This constrains the movie clip to a rectangle that enables you to drag it left or right (from a left boundary of 0 to a right boundary of 550), but it does not move up or down. Both the topSide and bottomSide variables are set to the Y position of the movie clip when it loads. This value is 75.

  5. Select frame 1 of the Actions layer once again. Create a new Sound object by adding the following ActionScript:

     //Create the sound object, attach the sound, and get it started  mySound = new Sound(this); 

You've created a new Sound object, named mySound , and set its target to this (the Volume Controller movie clip). You can test the movie (Control > Test Movie), but because you haven't actually attached or started a sound, the Volume Controller won't do anything more than slide back and forth on the screen as you drag it. So just save your file for now.



Inside Flash
Inside Flash MX (2nd Edition) (Inside (New Riders))
ISBN: 0735712549
EAN: 2147483647
Year: 2005
Pages: 257
Authors: Jody Keating

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