Categories
Featured jobs
» More ActionScript, Flash and Flex jobs.
» Advertise a job for free
Our network
Advertisement

 »  Home  »  Tutorials  »  Flash  »  Beginner  »  Drag 'n Drop and Drop Targets

Drag 'n Drop and Drop Targets

By Jesse Stratford | Published 09/9/2005 | Beginner | Rating:
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 Stratford
Page 1 of 2
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:

on (press) {
        startDrag (this, true);
}
on (release) {
        stopDrag ();
        if (this._droptarget == "/green_box") {
                _root.green_box.gotoAndStop(2);
        }
}

Ignore the If statement and all that follows it for the moment. That's the droptarget bit which we'll come back to. For now, take note of this bit:

on (press) {
        startDrag (this, true);
}
on (release) {
        stopDrag ();
}

Didn't I tell you it was ridiculously easy? This script says:

  1. When they press this button perform the following actions.
  2. 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).
  3. When the button is released, do this:
  4. Stop dragging the clip you're dragging.
How simple is that? It gets a bit more complicated on the next page...


Spread The Word / Bookmark this content

Clesto Digg it! Reddit Furl del.icio.us Spurl Yahoo!

Related Articles
Comments
  • 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.
     
Submit Comment



Search Entire Site
Add to Google
Advertisements
Article Options
Latest New Articles
Set up a simple IIS Server for Flash
by Peter McBride

Day 1 at FITC Toronto 2008
by Anthony Pace

Simple reflection effect with AS2
by Jean André Mas

ActionScript.org Meets Josh Tynjala (aka dr_zeus)
by ActionScript.org Staff

Rapidly Create Online Flash Movies to Help Users Market, Sell and Support Software and Hardware
by Sabrina F

mailing list
Enter your email address:
mailing list
Subscribe Unsubscribe
© 2000-2007 actionscript.org! All Rights Reserved.
Read our Privacy Statement and Terms of Use...
Our dedicated server is hosted and managed by WebScorpion Webhosting.