Create a smooth startDrag() in Flash

Let's face it, startDrag() has been around since wow....flash 4 I think? Perhaps Flash 3, not sure exactly. Sure it serves its purpose still, but one thing that has always bugged me about it ever since updateAfterEvent() became available, is the fact that startDrag is dependant of the frame rate.

I put together a simple MovieClip prototype that would work the same way as startDrag except using updateAfterEvent. It is pretty basic so you could probably add a lot to it, but it does perform the simple task of draggable Objects without being dependant upon the frame rate!

Actionscript:
  1. MovieClip.prototype.startSmoothDrag = function (lockCenter:Boolean) {
  2.     if (lockCenter == undefined) var lockCenter:Boolean = false;
  3.     var thisXMouse:Number = this._xmouse;
  4.     var thisYMouse:Number = this._ymouse;
  5.     var setPosition:Function = function (target:Object)
  6.     {
  7.         target._x = target._parent._xmouse - ((lockCenter) ? 0 : thisXMouse);
  8.         target._y = target._parent._ymouse - ((lockCenter) ? 0 : thisYMouse);
  9.     };
  10.     setPosition(this);
  11.     this.onMouseMove = function () {
  12.         setPosition(this);
  13.         updateAfterEvent();
  14.     };
  15. };
  16.  
  17. MovieClip.prototype.stopSmoothDrag = function () {
  18.     this.onMouseMove = null;
  19. };
  20.  
  21. myDraggableMC.onPress = function () {
  22.     this.startSmoothDrag(true);
  23. };
  24.  
  25. myDraggableMC.onRelease = myDraggableMC.onReleaseOutside = function () {
  26.     this.stopSmoothDrag();
  27. };

I have also put together a simple demo of this in use, to give you an idea of how it can be effective. Notice below that the fps (frames per second) is set to 1, which is slow if you didn't already know :). Notice how the smooth Dragable obejct is really smooth, while the regular startDrag() method is dependant on that horrifying 1 fps!

Click here to download.

Like I said, it is pretty simple but feel free to add to it what you want!

Digg This!

15 Responses to “Create a smooth startDrag() in Flash”

Pages: « 2 [1] Show All

  1. Andrew said,

    December 7, 2007 @ 7:28 pm

    Nice, but I’d really appreciate you adding the ability to constrain the draggable item as others have mentioned. This is a great function but without that addition it’s not so useful.

  2. Chris said,

    November 6, 2007 @ 5:20 am

    Is this for AS1? I keep getting an error with the updateAfterEvent() code. Does anyone know how to achieve the same effect but using CS3?

  3. tomi said,

    September 12, 2007 @ 9:30 pm

    How can I add boundries? the fuction only takes the boolean lock center

  4. Wouter said,

    July 4, 2007 @ 8:36 am

    Hey Steven,

    Thanks for publishing this code, it works great, I finally have a smooth drag&drop!

    Greets

  5. Jordan said,

    March 8, 2007 @ 1:50 pm

    so whats the trick for boundries? i have it kinda working, but it seems buggy.

  6. Steven Hargrove said,

    March 8, 2007 @ 10:16 am

    I blogged about it way back, here it is: http://www.stevenhargrove.com/ig-syntax-hiliter/ — great plugin

  7. Shane said,

    March 7, 2007 @ 9:14 am

    Great post!

    What do you use for displaying your code within a blog entry? I have been looking for something similar.

    Thanks.

  8. john docking said,

    February 19, 2007 @ 5:53 am

    I have just stumbled accross this code from a search engine request. Thanks for the code is fulfills the criteria perfectly.

    Thanks again.

    P.S. I’ll Be Back!

  9. contrid said,

    December 24, 2006 @ 7:34 am

    Hi there,

    This is great. Extremely useful.
    Thanks for the code.

  10. derek said,

    November 6, 2006 @ 4:37 pm

    Hey,
    Just wanted to say, simple as you said, but super useful. One thing I added that others may find useful is the left, top, right, bottom bounds so that it works in the same way as startDrag() but much smoother.

Pages: « 2 [1] Show All

Leave a Reply