Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Flash MX action script bible - Reinhardt R.

Reinhardt R. Flash MX action script bible - Wiley & sons , 2004. - 987 p.
ISBN: 0-7645-4354-7
Download (direct link): macractionscriptbiblefeb2004.pdf
Previous << 1 .. 273 274 275 276 277 278 < 279 > 280 281 282 283 284 285 .. 427 >> Next

On the CD-ROM
For this exercise, open (or make a copy of) the Sound_300_starter.fla file, located in the ch23 folder of the book's CD-ROM.
Now you will apply the overview notes to the starter document file. This starter file contains the same code you used in previous sections. There is already a loadFile() method that loads and plays an MP3 file.
1. Open the starter file from the book’s CD-ROM. The document’s stage contains all of the elements from the Sound_200.fla document. Save the document as Sound_300.fla.
2. Create a new layer on the main timeline and name it mcVolumeSlider. Place the layer at the bottom of the layer stack. Select frame 1 of this layer before you proceed to the next step.
3. Open the Library panel and drag an instance of the sliderClip symbol onto the stage. Place the instance in the right portion of the stage. In the Transform panel, rotate the instance -90 degrees (you can also use the Free Transform tool to do this). In the Property inspector, name the instance mcVolumeSlider. Resize the instance so that it fits within the height of the stage, as shown in Figure 23-16.
Figure 23-16: The mcVolumeSlider instance placed on the right edge of the stage
590 Part VI ♦ The Audio and Video Classes
4. Double-click the sliderClip symbol in the Library panel. In this symbol, the small circle to the left is a MovieClip instance named mcPos. Test the movie (Ctrl+Enter or ^+Enter) and you’ll see that you can click and drag the circle graphic on the slider’s rule. However, if you try to load an MP3 file into the movie, the slider does not do anything to the sound’s volume. In the next steps, you will modify the slider’s function to control the volume.
5. Go back to the Flash document and in the sliderClip timeline, select frame 1 of the actions layer. Open the Actions panel (F9) and review the code, which is also shown in Listing 23-6.
Listing 23-6: The sliderClip ActionScript Code
function setProp(nPos:Number, sProp:String, sndTarget:Sound):Void { var a:Number = nPos; var b:Number = 225; var c:Number = 100; var nPropVal:Number = (a/b)*c; sndTarget[sProp](nPropVal);
}
function startSlider(bActive:Boolean):Void { if(bActive){
this.onMouseMove = function(){
setProp(mcPos._x, sProp, sndTarget); updateAfterEvent();
};
}else{
this.onMouseMove = null;
}
}
mcPos._x = 112.5; mcPos.onPress = function():Void { this._parent.startSlider(true); this.startDrag(false,0,0,225,0);
};
mcPos.onRelease = mcPos.onReleaseOutside = function():Void{ this._parent.startSlider(false); this.stopDrag();
};
var sProp:String = "setVolume"; setProp(mcPos._x, sProp, sndTarget);
The setProp() function is executed by the onMouseMove() event handler created whenever the mcPos i nstance on the slider moves, as invoked by the onPress() handler and startSlider() function. The following line of code is a translation of the
setVolume() method of the Sound object:
sndTarget[sProp](nPropVal);
Here, sndTarget, an argument of the setProp() function, represents the snd_1 object created on the main timeline. sProp represents the current method of the snd_1 object
Chapter 23 ♦ The Sound Class 591
that you want to invoke. In this example, you want to use the setVolume() method. This method is declared as a String value in the following line of code:
var sProp:String = "setVolume";
The sProp value is passed to the setProp() function from the onMouseMove() handler defined in the startSlider() function. Finally, the nPropVal argument represents the volume level that you want to apply to the snd_1 object. With these actions in place, you can set the sndTarget variable for the mcVolumeSlider instance (needed for the proper operation of the setProp() function) from the actions on frame 1 of the main timeline, where the snd_1 object is created.
6. Go back to the main timeline and select frame 1 of the actions layer. In this frame’s actions, add the lines of bold code shown in Listing 23-7.
When the Flash movie starts, you want to tell the mcVolumeSlider instance which Sound object to monitor. The following line of code does just that:
mcVolumeSlider.sndTarget = snd_1;
The second new line of code executes the setProp() function within the mcVolumeSlider instance, passing the starting value of the mcPos instance, the method name to be enabled ("setVolume"), and the current sound target (snd_1).
mcVolumeSlider.setProp(mcVolumeSlider.mcPos._x, "setVolume", ^ snd_1);
Listing 23-7: The Modified Frame 1 Actions
function checkLoad(sndTarget:Sound):Void {
var nLB:Number = sndTarget.getBytesLoaded(); var nTB:Number = sndTarget.getBytesTotal(); var nPercent:Number = (nLB/nTB)*100; mcProgressBar._xscale = nPercent; if (nLB >= nTB && nTB > 0) { clearlnterval(nProgress);
}
}
var mcHolder:MovieClip = this.createEmptyMovieClip("mcHolder", 1); var snd_1:Sound = new Sound(mcHolder); mcVolumeSlider.sndTarget = snd_1;
mcVolumeSlider.setProp(mcVolumeSlider.mcPos._x, "setVolume”, snd_1);
var nProgress:Number;
Previous << 1 .. 273 274 275 276 277 278 < 279 > 280 281 282 283 284 285 .. 427 >> Next