Actionscript 3 Designer Toolkit

More design, less programming

Actionscript 3 Designer Toolkit header image 2

Changing variable values: the Slider Class

February 9th, 2010 · 2 Comments · AS3 Custom Classes

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)

Post to Twitter

Tags: ··

2 Comments so far ↓

  • tyler

    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

  • Charles Davis

    See what you’re saying… thanks. Changed .fla to correct and updated download.

Leave a Comment

Spam Protection by WP-SpamFree