It is fairly easy to figure out the code for a vertical volume slider that slides 100 pixels. However, what if you wanted your volume slider to be larger or smaller than 100 pixels? The code for this more complex (especially the formula for volCalc below). This one should respond automatically to changes in vertical height of the drag distance.
|Fig. 07: Example of vertical volume slider|
In this movie, a draggable knob with an instance name of vslider is positioned on top of another movie (the blue reticulated base). The base has the instance name of vbase. All of the code for volume control is placed in clip events of the movie vslider.
The knob, vslider, will automatically position itself in the center of the base (called "vbase" here). The variable "top" represents the uppermost limit for the slider. Since the slider is automatically centered over vbase, the top is set to be the slider's current position plus half the height of vbase (which can be any height).
In the load clip event, the exact height and x coordinate is set to make sure that there is no sub-pixel shift when published.
The volume is then set to: (_y-volCalc)*(-100/(_root.vbase._height))
This code is placed on the sliding knob movie (the red slider in the middle)
this._y = _root.vbase._y + _root.vbase._height/2 - this._height/2;
top= this._y + _root.vbase._height/2
bottom=this._y - _root.vbase._height/2
//End of onClipEvent load
_root.currentVolumeText="Volume: " + _root.myMusic.getVolume();
//End of onClipEvent enterFrame
startDrag(this, false, left, top, right, bottom)
// The following is a line by line explanation of the above.
Note: If your volume is one unit off (it slides up to 101, for example), change the height of your slider so that it is an even number, and make sure your vbase is on a whole even number on its Y-axis, and make the height of vbase an even number.
|» Level Intermediate|
Rating: 8 Votes: 31
|Kenny Bellew is a technical writer and freelance flash programmer who specializes in Flash audio. He lives in Minneapolis, MN.|
|Download the files used in this tutorial.|
|Download (12927 kb)|
|More help? Search our boards for quick answers!|