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.
Written by: Jesse Stratford Time: 15 minutes Difficulty Level: Beginner Requirements: Flash 4 or higher Topics Covered: startDrag(), stopDrag(), this element. Assumed knowledge: Instances, Paths.
Download the source for the above movie here. (Zipped, PC Flash 5 file).
This tutorial will deal briefly with how to create drag and drop operations which are pretty vital for interactive Flash sites nowadays. We will also discuss the _dropTarget property. Draggable clips are very useful and ridiculously easy to make. So let's begin!
Start by downloading the source file above or creating the following:
A new flash file
A movie clip with an instance name.
A button within that movie clip.
Now, if you check out the movie clip on the main stage that says "Please Drag Me" you will find that it contains a button. Edit the movie clip so you can get a good look at the button. Since we can't attach 'on' events, such as 'on (click)' to a movie clip and we can't drag a button because it has no (customizable) instance name we must combine a button and a movie clip to get the best of both worlds and produce a draggable object. If you examine the Actionscript on this button you will see that is is as follows:
Didn't I tell you it was ridiculously easy? This script says:
When they press this button perform the following actions.
Start dragging this clip. (Note that 'this' is the Flash 5 element which provides a relative path to the current object, I could also have centered "/clipName" where clipName was the instance name of the clip I want to drag, but that's not high tech enough for us is it?). The 'true' element here sets 'Lock Center' to true so that the mouse locks to the center of the movie clip, (I always think it looks neater that way).
When the button is released, do this:
Stop dragging the clip you're dragging.
How simple is that? It gets a bit more complicated on the next page...
Comment #1
(Posted by nik - Nikluins at telus.net) Rating
Thanks i needed that!
Comment #2
(Posted by Kris - kgienger at netfront.net) Rating
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.
Comment #3
(Posted by mahir - mahir.haroon at gmail.com) Rating
will this work for flash cs3
Comment #4
(Posted by rs - solidpsonge at hotmail.com) Rating
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?
Comment #5
(Posted by pete - pete at u4ea.net) Rating
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.