When Implementing Drag, Remember the Offsets!
September 8th, 2008 | View Comments
You use the drag gesture all the time! Many tasks today on your computer use a form of dragging such as moving icons on a desktop, rearranging items in my Start Menu, playing with cards in Solitaire, etc. If you use Flash or Blend, you can’t draw or reposition anything on the design surface without running into a drag action. This post isn’t really about recalling every instance of drag you probably encounter daily though. That would entertain me but bore all of you!
Instead, this post is about showing you how to create your own drag functionality. Writing your own drag functionality requires a trivial amount of code if you use Flash/ActionScript’s startDrag function. This function allows you to, in a single line, setup a drag action on a object you wish to drag. All of the details are taken care off for you.
For curiosity, have you ever thought about what you would need to do if you had to implement drag without relying on a built-in method? if you have (or if you haven’t!), this post will show you how, and below you will find an example:
In the above movie, click on the circle and drag your mouse to place your circle in a different location. For the sake of time, please download the source file from below so you can follow-along:
| Download Drag Source (Flash CS3) |
Once you have downloaded and opened the source file, you will see the following code:
- function Start() {
- dragButton.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
- dragButton.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
- stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
- }
- Start();
- var isDragging:Boolean = false;
- var xOffset:Number = 0;
- var yOffset:Number = 0;
- function mouseDownHandler(e:MouseEvent):void {
- isDragging = true;
- xOffset = e.localX;
- yOffset = e.localY;
- }
- function mouseUpHandler(e:MouseEvent):void {
- isDragging = false;
- }
- function mouseMoveHandler(e:MouseEvent):void {
- if (isDragging) {
- dragButton.x = mouseX - xOffset;
- dragButton.y = mouseY - yOffset;
- }
- }
I am not going to describe the code in greater detail in this post, but hopefully it is fairly readable. There is one thing that I will describe though – and that has to do with offsets.
Don’t Forget the Offsets
While Flash makes dragging really easy, many languages require you to write essentially what I wrote above for implementing drag. One thing that many implementations forget is to deal with the offset between where you click and your object’s registration point.
Every movie clip (or button or graphic) you create in Flash has its own set of co-ordinates for positioning content stored inside it. The origin of those co-ordinates where 0, 0 is defined is known as the registration point:
By default, your registration point is at the center of your object. Your click location, though, might not be exactly at the center. You may end up clicking somewhere away from the center such as in the location defined by the red circle shown below:
If you did not account for the difference between your registration point and your click point, what will happen is that your dragged item will jump to the point of your click. For smaller items, that jump might not be noticeable. For larger items, such as the circle in my example, the jump will be noticeable, and that can look a little weird.
The solution is to deal with the offset – the difference between where you clicked and your dragged object’s registration point:
If you look at my code, you will see that I am subtracting the offset value from the point where I click on my mouse. This allows me to drag without having to be locked to the position of the registration point but instead, dragging from the point of my click itself.
Failing to account for the offset is not the end of the world, but it is a minor detail that often bugs me. Hopefully, after reading this post, your implementations of drag will do the right thing and not bug me!
Cheers!
Kirupa





September 12th, 2008 at 1:52 pm
hi master,
I’m deciding to write to you here ’cause of a little drag and drop problem I can’t solve, and it seems no other also can resolve it on the forum…
I’ve built a simple coin-counter engine in AS2.0, mostly based on some of your tutorials.
The coins are scattered through the screen, and I need for any coin a variable x and y.
When I click on one of them, it go in my backpack and the coincounter variable adds one.
Problems come out when I retrieve it from the backpack: I’ve tried with attachMovie, it’s draggable but the stopDrag method do no go!!!
Well, my questions are all around in the forums (flash 8 and actionscript 1.0/2.0), I’m stuck with it… can I hope in a superior help?
thanking you a lot in advanace
Marcogiannini
September 12th, 2008 at 10:30 pm
That is a tricky one to solve without extra information. Have you tried implementing drag entirely yourself without relying on start/stopdrag? This post shows you how that can be done
February 27th, 2009 at 9:08 am
I just found your blog on google. I really liked it and now I will share it with my friends.
March 9th, 2011 at 2:39 am
I was questioning what is up with that bizarre gravatar??? I know 5am is early and I’m not wanting my finest at that hour, however I hope I do not seem like this! I’d nonetheless make that face if I am requested to do a hundred pushups. lol Anyway, in my language, there should not much good source like this.
March 9th, 2011 at 3:50 am
I used to be more than happy to search out this site.I wished to thank you for this nice read!! I definitely enjoying each little little bit of it and I’ve you bookmarked to take a look at new stuff you post. Anyway, in my language, there aren’t a lot good supply like this.
March 9th, 2011 at 4:32 am
I used to be wondering if you want to be a guest poster on my web site? and in trade you may include a link your submit? Please reply whenever you get a chance and I will ship you my contact particulars – thanks. Anyway, in my language, there will not be much good supply like this.
April 27th, 2011 at 4:29 am
I was questioning if cabbage soup dietyou need to be a guest poster on my web site? cabbage soup dietand in change you would embody a link your put up? cabbage soup dietPlease reply while you get an opportunity and I cabbage soup diet will ship you my contact details – thanks. cabbage soup diet Anyway, in my language, there will not be much good source like this.cabbage soup diet
July 29th, 2011 at 8:10 am
Hello there! Quick question that’s totally off topic. Do you know how to make your site mobile friendly? My site looks weird when browsing from my apple iphone. I’m trying to find a theme or plugin that might be able to fix this issue. If you have any suggestions, please share. Thank you!
August 20th, 2011 at 11:29 am
Do you mind if I quote a couple of your articles as long as I provide credit and sources back to your blog? My website is in the exact same niche as yours and my visitors would definitely benefit from some of the information you present here. Please let me know if this ok with you. Thank you!
November 22nd, 2011 at 3:13 am
I was really pulling for Hope and Max (love Max!), but I believe the Kardashian-s will be crashing the finals party. Rob is doing very well, and the family takes every opportunit-y to promote itself on the show.runescape generator,runescape grand exchange,runescape hunter