
Page 2 of 2
Jesse Stratford
Jesse lives and works in Melbourne Australia. He is the Cofounder and a Director of http://ActionScript.org. A Flash enthusiast, teacher, author, freelancer and speaker Jesse enjoys participating in the http://ActionScript.org community and the wider Flash scene when he has time.
View all articles by Jesse StratfordIf you examine the pink button it has very similar code except it also includes constraints on movements. This allows us to limit the area in which the clip may be dragged. This is useful for singular plain (along _x or along _y) dragging only, or when we don't want people dragging one window over another. The syntax is as follows:
on (press) {
startDrag (this, true, 30, 80, 140, 110);
}
on (release) {
stopDrag ();
}
Notice it's very much the same except for the numbers which follow the 'true'. These numbers are the bounds of the rectangle within which we will allow users to drag the clip. (Yes, you must define a rectangle, no circles or triangles, it's four points people!). From left to right the numbers are: left, top, right, bottom. These are measured in pixels from the top left corner of the movie where _x and _y both equal 0.
Finally, the _droptarget property allows us to detect where a user has dropped a draggable clip. Actually it allows us to detect if a user has dropped the clip on another clip with a name, and if so, upon which. This operation is as easy as adding the following lines of code:
on (release) {
stopDrag ();
if (this._droptarget == "/green_box") {
_root.green_box.gotoAndStop(2);
}
}
The IF statement here checks if the _droptarget property (see Get Property tutorial) of the draggable clip is equal to the string "/green_box". "/green_box" is the path to another element in my sample movie above. If the users drops the clip on the clip with the instance name "green_box", the _droptarget property evaluates to the full path to that object, in this case "/green_box". Note that "/green_box" is a Flash 4 format path (see the paths tutorial for more on this), which is necessary for backward compatibility of the Flash player. The next line just uses tell targets to send my green_box clip forward one frame.
And that's it!
| Jesse Stratford is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash. NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there. |
If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity. |
| This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it. |
Spread The Word
Related Articles
5 Responses to "Drag 'n Drop and Drop Targets" 
|
said this on 13 Oct 2007 3:30:59 AM CDT
You comment that we should 'see the paths tutorial for more on this' however it would be very helpful if that text was infact linked to that lesson so one could flip back and forth when trying to make sense of this action.
|
|
said this on 30 Oct 2007 6:21:38 AM CDT
will this work for flash cs3
|
|
said this on 06 Jan 2008 10:01:29 AM CDT
i cant get this to work when the whole setup is within a movieclip too. I used _parent. instead of root but for some reason its not working?
|
|
said this on 21 Feb 2008 9:07:49 AM CDT
in MX 2004, the release stopdrag won't work unless you take the second parameter "true" in the startDrag() method out. i guess the parameter has changed since version 5.
|



Author/Admin)