Flash: Slider Class
June 16th, 2005 by LuisI quite like to use sliders in order to test some values while I'm developing a project.
Until now I've been using a simple draggable movie clip with startDrag() method. Sometimes I need to ease or jump to certain values, so this morning I decided to create a simple and quick Slider class with simple ease and jump to value features.
Sample swf:
[flash]http://www.blog.lessrain.com/wp-content/upload/slider.swf,400,250[/flash]
Click here to download the files
Actionscript:
- /**
- * @class: Slider
- * @author: luis@lessrain.com
- * @version: 1
- * -----------------------------------------------------
- * Basic Slider
- * ----------------------------------------------------
- */
- import utils.Proxy;
- import animation.Tween;
- import mx.events.EventDispatcher;
- class Slider
- {
- private var _knobMC:MovieClip;
- private var _baseMC:MovieClip;
- private var _orientation:String;
- private var _prop1:String;
- private var _prop2:String;
- private var _left:Number;
- private var _right:Number;
- private var _top:Number;
- private var _bottom:Number;
- private var pointX:Number;
- private var pointY:Number;
- private var mouseListener:Object;
- private var _initValue:Number;
- private var _endValue:Number;
- private var _range:Number;
- private var _value:Number;
- private var _tw:Tween;
- //private static var overColor:Number=0xFFFFFF;
- //private static var outColor:Number=0xFF004C;
- // required for EventDispatcher:
- private var dispatchEvent:Function;
- public var addEventListener:Function;
- public var removeEventListener:Function;
- function Slider ()
- {
- EventDispatcher.initialize (this);
- }
- public function init (knob:MovieClip, base:MovieClip, orientation:String, initValue:Number, endValue:Number):Void
- {
- _knobMC = knob;
- _baseMC = base;
- _knobMC._x=_baseMC._x;
- _knobMC._y=_baseMC._y;
- _orientation = orientation;
- if(_orientation == 'horizontal'){
- _prop1='_x';
- _prop2='_width';
- }else{
- _prop1='_y';
- _prop2='_height'
- }
- _initValue = initValue;
- _endValue = endValue;
- _range = (_endValue - _initValue);
- //_knobMC.onRollOver = Proxy.create (this, colorize, _knobMC, overColor);
- //_knobMC.onRollOut = Proxy.create (this, colorize, _knobMC, outColor);
- _knobMC.onPress = Proxy.create (this, startDrag);
- _knobMC.onRelease = _knobMC.onReleaseOutside = Proxy.create (this, stopDrag);
- _baseMC.onPress = Proxy.create (this, findValue);
- _knobMC.useHandCursor = _baseMC.useHandCursor = false;
- _tw=new Tween(_knobMC);
- redrawLimits ();
- }
- private function startDrag ():Void
- {
- //colorize (_knobMC, overColor);
- _tw.abort();
- pointX = _knobMC._x - _xmouse;
- pointY = _knobMC._y - _ymouse;
- mouseListener = new Object ();
- mouseListener.onMouseMove = Proxy.create (this, updatePos);
- Mouse.addListener (mouseListener);
- dispatchEvent ({target:this, type:'onStartDrag'});
- }
- private function stopDrag ():Void
- {
- Mouse.removeListener (mouseListener);
- //colorize (_knobMC, outColor);
- dispatchEvent ({target:this, type:'onStopDrag'});
- }
- private function updatePos ():Void
- {
- var pos:Number = getCurrentValue (_knobMC[_prop1]);
- if (pos>= _initValue && pos <= _endValue) {
- _knobMC._x = Math.round (_xmouse + pointX);
- _knobMC._y = Math.round (_ymouse + pointY);
- }
- checkLimits ();
- updateAfterEvent ();
- }
- private function checkLimits ():Void
- {
- if (_knobMC._x <_left) _knobMC._x = _left;
- if (_knobMC._x> _right) _knobMC._x = _right;
- if (_knobMC._y <_top)_knobMC._y = _top;
- if (_knobMC._y> _bottom) _knobMC._y = _bottom;
- updateValue();
- }
- public function redrawLimits ():Void
- {
- _left = _baseMC._x;
- _top = _baseMC._y;
- _right = Math.round((_baseMC._x + _baseMC._width) - (_knobMC._width));
- _bottom = Math.round((_baseMC._y + _baseMC._height) - (_knobMC._height));
- checkLimits ();
- }
- public function getTarget ():MovieClip
- {
- return _knobMC;
- }
- public function getCurrentValue (ref:Number):Number
- {
- return (Math.round ((ref - _baseMC[_prop1]) / (_baseMC[_prop2] - _knobMC[_prop2]) * _range + _initValue));
- }
- public function jumpTo (val:Number):Void
- {
- _knobMC[_prop1] = translateValToPos(val)
- checkLimits ();
- }
- private function translateValToPos():Number
- {
- var tempValue:Number;
- if(arguments.length){
- tempValue=arguments[0];
- }else{
- tempValue=(_orientation == 'horizontal')? getCurrentValue(_xmouse-(_knobMC[_prop2]/2)):getCurrentValue(_ymouse-(_knobMC[_prop2]/2));
- }
- return (Math.round (_baseMC[_prop1] + (((_baseMC[_prop2] - _knobMC[_prop2]) * (tempValue - _initValue)) / (_range))));
- }
- private function findValue():Void
- {
- _tw.abort();
- _tw.deleteProperties();
- _tw.duration=250;
- _tw.setTweenProperty (_prop1, _knobMC[_prop1], translateValToPos());
- _tw.addEventListener('onProgress',Proxy.create(this,checkLimits));
- _tw.addEventListener('onComplete',Proxy.create(this,checkLimits));
- _tw.easingFunction=mx.transitions.easing.Regular.easeIn
- _tw.start();
- }
- private function updateValue():Void{
- dispatchEvent ({target:this, type:'onChange', value:getCurrentValue (_knobMC[_prop1])});
- }
- private function enableItems(enable:Boolean):Void
- {
- _knobMC.enabled=_baseMC.enabled=enable;
- }
- private function colorize (mc:MovieClip, col:Number):Void
- {
- var c:Color = new Color (mc);
- c.setRGB (col);
- }
- }


June 16th, 2005 at 3:01 pm
This looks rather interesting I’ll have to play around with it. Thanks for taking the time to share with the world
June 16th, 2005 at 3:10 pm
Hi Luis,
Very good class!!
The link to download sample files is broken
greetings,
Emiliano Velasco
June 16th, 2005 at 3:13 pm
The link works now.
June 16th, 2005 at 3:20 pm
The download link works for me.
Gtreat work, this is some thing that always useful
June 16th, 2005 at 5:54 pm
great!
well done m8
hey, I sent you an email to your hotmail account, hope to see you 2nite
cheers
August 16th, 2005 at 10:08 am
August 16th, 2005 at 10:42 am
September 16th, 2005 at 8:28 am
very cool stuff !
November 9th, 2005 at 9:32 am
nice code but as i’m still quite new to AS (i’ve done some OOPS programming but not that intensive in flash), so if you can write a little more details on your approach, it’ll be really beneficial to everyone else.
I’m actually writing my own slider from scratch but it seems like i’ll have to spend quite a bit of time to get things working, but my code will for sure be ugly and case-specific, impossible to reuse. that’s why i’d like to see your approach to improve my design — hope you don’t mind
cheers and thanks for sharing!
May 10th, 2006 at 2:49 pm
hi
anybody have the sample files?
thanks.
October 20th, 2007 at 12:29 pm
thx, http://www.avu.cz/chronik
f-r-a