The Slider class is used for linearly changing the value of a variable such as volume or speed.
User defined parameters define the length of the slider track, the minimum and maximum values returned and the slider caption. Another custom class, SliderValueEvent is used in conjunction with the Slider to output the values as seen in the .fla below.
// import required custom classes import com.dtk.Slider; import com.dtk.SliderValueEvent; import com.dtk.HTMLText; // instance of Slider class - parameters: track length, min value, max value, caption var s:Slider = new Slider( 240, 1, 10, 'Speed' ); s.x = 40; s.y = 40; s.addEventListener( SliderValueEvent.VALUE_CALCULATED, getValues ); addChild( s ); // function called by event listener function getValues( e:SliderValueEvent ):void { // remove previous instance of HTMLText removePrevious( 'tx' ); // display value (e.sliderValue) using HTMLText class var d:HTMLText = new HTMLText( 100, 'Arial', 36, 0x999999, 'RIGHT', ' ' + e.sliderValue.toString( ) ); d.x = 280; d.y = 14; d.name = 'tx'; addChild( d ); } // function to remove previous instances of a named class function removePrevious( nameOfClass:String ):void { for ( var i:uint = 0; i < numChildren; i++ ) { if ( getChildAt( i ).name == nameOfClass ) { removeChildAt( i ); } } } |
The download below includes the .fla file and the 3 class files used above.
Download files: Slider.zip (51)
cool stuff. I am wondering why, if the max value is 10, its allowed to go to 11. Also, is there a reason that you are instantiating a new “HTMLText” text field with each update event? its my first slider, thanks
See what you’re saying… thanks. Changed .fla to correct and updated download.