制作Slider组件
在gotoAndLearn上看到一个制作Slider的教程,感觉挺酷的
package
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.events.Event;
public class ThrowSllider extends MovieClip
{
private var _bounds:Object;
private var _currentX:Number;
private var _lastX:Number;
private var _offset:Number;
private var _isDragging:Boolean;
private var _vx:Number;
private var _thumb:MovieClip;
private var _track:MovieClip;
private var _percent:TextField;
public function ThrowSllider()
{
initView();
_bounds = {left:35,right:235};
_currentX = _thumb.x;
_lastX = _thumb.x;
_offset = 0;
_isDragging = false;
_vx = 0;
_thumb.buttonMode = true;
_thumb.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
stage.addEventListener(MouseEvent.MOUSE_UP,onUp);
stage.addEventListener(Event.ENTER_FRAME,onLoop);
}
private function initView():void
{
_thumb = this.getChildByName("thumb") as MovieClip;//舞台thumb实例
_track = this.getChildByName("track") as MovieClip;//舞台track实例
_percent = this.getChildByName("percent") as TextField;//舞台percent实例
}
private function onLoop(e:Event):void
{
if (_isDragging)
{
_lastX = _currentX;
_currentX = mouseX;
_vx = _currentX - _lastX;
}
else
{
_thumb.x += _vx;
}
if (_thumb.x <= _bounds.left)
{
_thumb.x = _bounds.left;
_vx *= -1;
}
else if (_thumb.x>=_bounds.right)
{
_thumb.x = _bounds.right;
_vx *= -1;
}
_vx *= 0.95;
_percent.text = Math.round((_thumb.x-_bounds.left)/(_bounds.right-_bounds.left) *100).toString()+"%";
}
private function onDown(e:MouseEvent):void
{
_isDragging = true;
_offset = _thumb.mouseX;
stage.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
}
private function onMove(e:MouseEvent):void
{
_thumb.x = mouseX - _offset;
if (_thumb.x <= _bounds.left)
{
_thumb.x = _bounds.left;
}
else if (_thumb.x>=_bounds.right)
{
_thumb.x = _bounds.right;
}
e.updateAfterEvent();
}
private function onUp(e:MouseEvent):void
{
_isDragging = false;
stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMove);
}
}
}

浙公网安备 33010602011771号