Posted By

arpo on 01/22/09


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

enajenkins
davidino1978


Basic slider


 / Published in: ActionScript 3
 

  1. var handle:Sprite;
  2. var base:Sprite;
  3. var dragRec:Rectangle;
  4. var dir:String;
  5. var _value:Number;
  6.  
  7. base = new Sprite();
  8. base.graphics.beginFill(0xdddddd);
  9. base.graphics.drawRect(0, 0, 400, 40);
  10. base.graphics.endFill();
  11. base.x=100;
  12. base.y=100;
  13. addChild(base);
  14. addSlider(base);
  15.  
  16. function addSlider(target:Sprite){
  17.  
  18. var handleW:Number = 80;
  19. var handleH:Number = 40;
  20. var sbW:Number = 0;
  21. var sbH:Number = target.height - handleH;
  22. dir = "v";
  23.  
  24. if(target.width>target.height){
  25. sbW = target.width - handleW;
  26. sbH= 0;
  27. dir = "h";
  28. }
  29.  
  30. handle = new Sprite();
  31. handle.graphics.beginFill(0x0000ff);
  32. handle.graphics.drawRect(0, 0, handleW, handleH);
  33. handle.graphics.endFill();
  34. handle.name = "handle";
  35. handle.buttonMode = true;
  36. handle.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  37. handle.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
  38. dragRec = dragRec = new Rectangle(handle.x, handle.y, sbW, sbH);
  39. target.addChild(handle);
  40.  
  41. }
  42.  
  43. function mouseUp(event:MouseEvent):void {
  44. if (event.target != handle){
  45. doStopDrag(handle);
  46. }
  47. stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
  48. }
  49.  
  50. function mouseDownHandler(event:MouseEvent):void {
  51. var sprite:Sprite = Sprite(event.target);
  52. stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
  53. stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
  54. sprite.startDrag(false, dragRec);
  55. }
  56.  
  57. function mouseUpHandler(event:MouseEvent=null):void {
  58. var sprite:Sprite = Sprite(event.target);
  59. doStopDrag(sprite);
  60. }
  61.  
  62. function doStopDrag(clip:Sprite):void {
  63. clip.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
  64. stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
  65. clip.stopDrag();
  66. }
  67.  
  68. function mouseMoveHandler(event:MouseEvent=null):void {
  69.  
  70. if(dir=="v"){
  71. _value = (base.height - handle.height - (base.height - handle.height - handle.y)) * base.height/(base.height - handle.height);
  72. _value = _value/base.height;
  73. }else{
  74. _value = (base.width - handle.width - (base.width - handle.width - handle.x)) * base.width/(base.width - handle.width);
  75. _value = _value/base.width;
  76. }
  77.  
  78. trace(_value)
  79.  
  80. //dispatchEvent(new Event(CustomEvents.SLIDER_UPDATE));
  81. }
  82.  
  83. function setValue($value):void
  84. {
  85.  
  86. if (dir == "v") {
  87.  
  88. handle.y = (base.height - handle.height) * $value;
  89.  
  90. }else {
  91.  
  92. handle.x = (base.width - handle.width) * $value;
  93.  
  94. }
  95.  
  96. }

Report this snippet  

You need to login to post a comment.