Actionscript Drag & Drop Tutorial | Vertical and Horizontal Flash Sliders

    as3dragdrop-slider png A specific use of drag and drop which is a bit more complicated than your average drag & drop needs is a slider. You can use components, but I usually prefer using my own graphics and code, partly because the components tend to bloat the filesize of the swf and partly because that’s just how I am, I like to make it myself. Many projects I’ve worked on require sliders as a form of user input, such as a volume control in my video player, or the inputs for my Voter’s Aide app that let users assign value to issues in the 2008 presidential election. I figured I’d just pull out the code I used with the sliders there, since it was already done. The issue with sliders is we need to restrict the dragging to a certain area, which in itself is a line of code, but I also prefer to allow users to click the actual bar as well for quick selection.

    Example

    Get Adobe Flash player

    Vertical Slider Steps

    The vertical slider here goes from 0 – 100. We need to drag the handle but have it restricted to the slider, so users won’t be confused when they click and drag the handle off the slider and break it. We want to click the background bar of the slider and have the handle snap to that place, and we need to be able to see what value the slider holds (0 – 100). I made this code to be pretty reusable, as long as the slider is set up in similar fashion.

    1. Make graphics for slider bg and handle
    2. Put the graphics into a slider mc
    3. Place them each at 0,0 and center their registration points (for easier control and code later)
    4. Assign button mode to handle and bar (for better usability)
    5. Add Mouse Down Event Listener for handle and bar and assign press function
    6. In bar press function set position of handle according to mouse position, and then call the handle press function
    7. In handle press function remove the Mouse Down listeners and add stage mouse event listeners for both mouse Up and Move (Stage listeners emulate onReleaseOutside (from as2) and also provide more accurate results)
    8. Define dragging area as a rectangle(x, y, width, height), if you’ve do the set up earlier it should be close to Rectangle(0,0,0,slider.bar.height);
    9. Begin dragging handle and apply the drag area limiting rectangle
    10. Mouse Move function find value (should simply be the handle’s y position) and updateAfterEvent for smooth animation
    11. Mouse Release function remove stage listeners, re-add the listeners to the slider and stop dragging

    Actionscript (as3)

    // Vertical Slider
    sliderVertical.handle.buttonMode = true;
    sliderVertical.bar.buttonMode = true;
    sliderVertical.handle.addEventListener(MouseEvent.MOUSE_DOWN, verticalHandlePress);
    sliderVertical.bar.addEventListener(MouseEvent.MOUSE_DOWN, verticalBarPress);

    function verticalBarPress(e:MouseEvent):void{
        sliderVertical.handle.y = sliderVertical.mouseY;
        verticalHandlePress(e);
    }
    function verticalHandlePress(e:MouseEvent):void {
        sliderVertical.handle.removeEventListener(MouseEvent.MOUSE_DOWN, verticalHandlePress);
        sliderVertical.bar.removeEventListener(MouseEvent.MOUSE_DOWN, verticalBarPress);
        stage.addEventListener(MouseEvent.MOUSE_UP, verticalHandleRelease);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, verticalHandleDrag);
        //limit dragging area
        var verticalDragArea:Rectangle = new Rectangle(0, 0, 0, -sliderVertical.bar.height+1);
        sliderVertical.handle.startDrag(false, verticalDragArea);
    }
    function verticalHandleRelease(e:MouseEvent):void{
        stage.removeEventListener(MouseEvent.MOUSE_UP, verticalHandleRelease);
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, verticalHandleDrag);
        sliderVertical.bar.addEventListener(MouseEvent.MOUSE_DOWN, verticalBarPress);
        sliderVertical.handle.addEventListener(MouseEvent.MOUSE_DOWN, verticalHandlePress);

        sliderVertical.handle.stopDrag();
        updateVNumber();
    }
    function verticalHandleDrag(e:MouseEvent):void{
        e.updateAfterEvent();
        updateVNumber();
    }
    function updateVNumber():void{
        sliderVertical.sliderValue = sliderVertical.stat.htmlText = Math.abs(sliderVertical.handle.y);
        sliderVertical.stat.y = sliderVertical.handle.y - sliderVertical.handle.height/2;
    }

    Horizontal Slider Steps

    Pretty much the same as the vertical slider, but adjust heights and y positions to widths and x positions. Note in this example I have a range of (-100 to 100) and to accomplish the bar I just reused the same on flipping it around, so here we have the handle, the barLeft and the barRight. I use both of these combined to calculate the limiting rectangle area.

    Actionscript (as3)

    // Horizontal Slider
    sliderHorizontal.handle.buttonMode = true;
    sliderHorizontal.barLeft.buttonMode = true;
    sliderHorizontal.barRight.buttonMode = true;
    sliderHorizontal.handle.addEventListener(MouseEvent.MOUSE_DOWN, horizontalHandlePress);
    sliderHorizontal.barLeft.addEventListener(MouseEvent.MOUSE_DOWN, horizontalBarPress);
    sliderHorizontal.barRight.addEventListener(MouseEvent.MOUSE_DOWN, horizontalBarPress);

    function horizontalBarPress(e:MouseEvent):void{
        sliderHorizontal.handle.x = sliderHorizontal.mouseX;
        horizontalHandlePress(e);
    }
    function horizontalHandlePress(e:MouseEvent):void {
        sliderHorizontal.handle.removeEventListener(MouseEvent.MOUSE_DOWN, horizontalHandlePress);
        sliderHorizontal.barLeft.removeEventListener(MouseEvent.MOUSE_DOWN, horizontalBarPress);
        sliderHorizontal.barRight.removeEventListener(MouseEvent.MOUSE_DOWN, horizontalBarPress);
        stage.addEventListener(MouseEvent.MOUSE_UP, horizontalHandleRelease);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, horizontalHandleDrag);
        //limit dragging area
        var dragArea:Rectangle = new Rectangle(-sliderHorizontal.barLeft.width+1, 0, sliderHorizontal.barLeft.width+sliderHorizontal.barRight.width-2, 0);
        sliderHorizontal.handle.startDrag(false, dragArea);
    }
    function horizontalHandleRelease(e:MouseEvent):void{
        stage.removeEventListener(MouseEvent.MOUSE_UP, horizontalHandleRelease);
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, horizontalHandleDrag);
        sliderHorizontal.handle.addEventListener(MouseEvent.MOUSE_DOWN, horizontalHandlePress);
        sliderHorizontal.barLeft.addEventListener(MouseEvent.MOUSE_DOWN, horizontalBarPress);
        sliderHorizontal.barRight.addEventListener(MouseEvent.MOUSE_DOWN, horizontalBarPress);

        sliderHorizontal.handle.stopDrag();
        updateHNumber();
    }
    function horizontalHandleDrag(e:MouseEvent):void{
        e.updateAfterEvent();
        updateHNumber();
    }
    function updateHNumber():void{
        sliderHorizontal.sliderValue = sliderHorizontal.stat.htmlText = sliderHorizontal.handle.x;
        sliderHorizontal.stat.x = sliderHorizontal.handle.x - sliderHorizontal.handle.width;
    }

    Source

    source as3dragdrop-sliders.fla file

    This entry was posted in tutorial and tagged , , , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

    5 Comments

    1. buzz
      Posted July 1, 2009 at 9:39 pm | Permalink

      nice,

      needs an onReleaseOutside event.. then it would be money ;)

    2. circlecube
      Posted July 2, 2009 at 6:47 am | Permalink

      @Buzz – Check step 7:

      In handle press function remove the Mouse Down listeners and add stage mouse event listeners for both mouse Up and Move (Stage listeners emulate onReleaseOutside (from as2) and also provide more accurate results)

    3. matanume
      Posted November 15, 2011 at 8:13 pm | Permalink

      Isn’t startDrag(false,new Rectangle(e.target.x,0,0,stage.height)); //vertical
      enough?

      • matanume
        Posted November 15, 2011 at 8:16 pm | Permalink

        ops i’m blind = =’

    4. Posted December 25, 2011 at 9:53 pm | Permalink

      thanks man…
      that was useful

      need it for a grid movement

    Post a Comment

    Your email is never published nor shared. Required fields are marked *

    *
    *

    You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    • Recent Posts

      Synth Emulator on Google Japan Doodle Today

      Synth Emulator on Google Japan Doodle Today Embedded Link Moog 自分のオリジナル曲を創って、 #moogdoodle で共有しよう。 Tweet

      WordPress updates plugin directory

      New additions to the plugin directory include: favorites, incorporating support forums into it's own tab for each plugin as well as support stats being displayed! Great! I think we also need the ability to give plugins ratings and reviews (bonus points if it can be done from within a wordpress admin dashboard when installing plugins). [...]

      Short Head

      Use zipf's short head to tune your website rather than redesign the whole thing. To make a website successful it needs to meet the needs of the users. Find out what those needs are by using the short head philosophy to equate most searched things as the biggest needs of the users. Use personas to [...]

      Img Set?

      Great article at a list apart discusing the state of the industry regarding responsive images. This picks apart the set attribute of the img element from a surprisingly objective view coming from someone so close to the picture element. Insightful discussion about the principle behind the proposals than the actual solution too. If the working [...]

      Triudo

      A mesmerizing animated triangle-ish shape form. Embedded Link triduo triduo Tweet

      Git – the paradigm shift

      A great developer story about the differences on what Git is vs other version control and what Git is not. This is how we should learn it. I heard over and over that it was distributed, but never grasped what that meant, so here are a few links and explanations that will help unlearn version [...]

      Tweening Lib comes to Javascript!

      I'm very excited to share the news that the tween library from GreenSock (hands down the best tweening library I used in flash) is not ported for use in javascript! This will be great! I missed that simple syntax from as3 when animating javascript, and now I can have my cake and eat it too. [...]

      Responsive CSS Tricks

      Here are a few useful css tricks to remember when building responsive design sites from web designer wall Embedded Link 5 Useful CSS Tricks for Responsive Design Making the design to be responsive is very easy as shown in my Responsive Design in 3 Steps tutorial, but maintaining the elements to look aesthetically balanced on [...]

      Picture element of srcset attribute?

      Bruce details the reasons and story behind the srcset attribute which is now introduced as an alternative to the picture element. Some aspects of the attribute are nice (like the fact that it's an attribute and not a new element, so it's creating up new elements with for problems. It's adapting currently used elements to [...]

      SVG Preloader with Raphael JS

      Here's a very creative use of using a newly available technology. Using svg graphics which are very lightweight, for a website preloader. I like the animation used as well. Embedded Link Make a stylish preloader with SVG | Tutorial | .net magazine Many sites neglect users with slow connections. Ian Culshaw explains how to use [...]

    • Recent Comments

      Bruce Brownlee

      Bruce Brownlee

      Ah IE6. I'd have 2 more years of sleep without IE6. Margin doubling, no properties,...
      versaena

      versaena

      how to give color at runtime…… thank you
      Mobile Websites

      Mobile Websites

      I disagree, mobile websites are the future – desktop websites and mobile websites...
      Matt Fasick

      Matt Fasick

      That's cool. I like the ripple effect as well.
      Nico

      Nico

      hi! really great job guy! very impressive.. just a question… do u have a solution to do a refresh...
      Evan Mullins

      Evan Mullins

      Agreed! I've just seen some people get pretty heated about separating all functionality...