Dragging and Dropping Movie Clip Instances
In user interfaces, it's sometimes useful to add drag-and-drop behaviors to movie clip instancesa term that refers to the process of clicking and dragging movie clip instances around the stage, so that when they're released (dropped), they'll perform a set of actions determined by the location where they're dropped. The easiest way to conceptualize this type of behavior is by thinking of the trashcan icon on your computer desktop: If you drag a file over the trashcan and let go of it, that file is deleted; however, if you're not over the trashcan when you let go, you've simply moved the file icon.The most common way to create drag-and-drop items in Flash is by using _droptarget or hitTest(). Accessing the _droptarget property of a movie will return the path in slash syntax to the highest movie clip instance that the currently dragged movie clip instance is over. Using the hitTest() method, you can determine whether the bounding box of one instance is touching the bounding box of another and take action accordingly. For more on this topic, see Lesson 8, "Using Conditional Logic." The hitTest() method is used more frequently because it's more versatile than the _droptarget property.In this exercise, you'll extend your project to dynamically create a row of iconssimple graphical movie clip instancesthat can be dragged and dropped onto the canvas_mc movie clip instance using hitTest(). When dropped, a copy of the movie clip instance will be created using duplicateMovieClip() and the original will be sent back to its initial position.
Open draw3.fla.This file is as you left it at the end of the preceding exercise. You will continue to add ActionScript to the _root timeline in this exercise. You'll create a function that creates the row of icons below the canvas_mc movie clip instance. Then you'll add the ActionScript that makes them draggable and the ActionScript that detects whether they were dropped onto canvas_mc.With the Actions panel open, select Frame 1 of the Actions layer and add the following function:
Although this function is fairly large, there's nothing here that you haven't seen before. The icon_mc movie clip instance contains a certain number of frames on its timeline, each of which includes a different icon graphic. This script duplicates the icon_mc clip once for every frame in that movie clip. It sends each duplicated movie clip instance to a unique frame and aligns it along the bottom of the screen. The result is a row of icons at the bottom of the screen. You can add or remove icons (that is, add or remove frames) in the icon_mc movie clip instance, and this loop will adapt based on the _totalframes property used in the for loop. The spacing variable represents the vertical space between the duplicated icons. iconY and iconX represent the coordinates of the first duplicate.
function buildIconList() {
var spacing:Number = 85;
var iconY:Number = 360;
var iconX:Number = 70;
for (var i = 0; i < _root.icon_mc._totalframes; ++i) {
var newName:String = "icon_mc" + i;
var clip:MovieClip = _root.icon_mc.duplicateMovieClip(newName, 10000 + i);
clip.gotoAndStop(i + 1);
clip._x = iconX + i * spacing;
clip._y = iconY;
clip.homeX = clip._x;
clip.homeY = clip._y;
clip.icon_btn.onPress = function() {
startDrag(this._parent);
};
clip.icon_btn.onRelease = function() {
stopDrag();
_root.iconReleased(this._parent);
};
}
}

This will call the function you created in Step 2 to create the icon list.Add the following function at the end of Frame 1:
buildIconList();
This function is set up to receive one parameter, which is identified as icon. This parameter is a reference to the icon movie clip instance that has just been dragged and dropped. The value of this parameter plays an important role in how the rest of the function works.A conditional statement checks whether the mouse pointer is over the canvas_mc movie clip instance when the function is called, indicating that the icon has actually been dropped on the canvas. If the mouse pointer is over canvas_mc, a copy of the movie clip instance that's dragged and dropped is created using duplicateMovieClip(). The name given to the duplicate is derived by concatenating "object" with the current value of iconDepth (which is defined in the next step, and is incremented with each new duplicate created) and adding "_mc" to the end. Based on this functionality, the current value of iconDepth will always reflect the number of duplicate icons that have been dragged and dropped onto the canvasan important thing to remember for the next exercise.After it's created, the duplicate is sent to the same frame as the original instance so that the same icon that was dragged appears on the canvas. The next action is used to disable the invisible button inside the duplicate named icon_btn. In addition, the duplicate is scaled vertically and horizontally by 250 percent so that it will appear on the canvas as a larger representation of the icon instance from which it was created.
function iconReleased(icon:MovieClip) {
if (_root.canvas_mc.hitTest(_root._xmouse, _root._ymouse)) {
++iconDepth;
var newName:String = "object" + iconDepth + "_mc";
var clip:MovieClip = icon.duplicateMovieClip(newName,iconDepth);
clip.gotoAndStop(icon._currentFrame);
clip.icon_btn.enabled = false;
clip._xscale = 250;
clip._yscale = 250;
}
icon._x = icon.homeX;
icon._y = icon.homeY;
}
NOTEA duplicated instance inherits the exact x and y positions of the original at the time of its duplication; therefore, unless you use a simple script to move it, a duplicateupon its creationis placed right on top of the original. This means you won't be able to tell that a duplicate has been created because nothing will have changed visually.The last two lines of ActionScript in this function send the dragged movie clip instance back to its original position, based on the values of homeX and homeY on its timeline. Notice that these actions are placed outside the conditional statement, meaning that they're executed regardless of whether the statement proves true and a duplicate is created. If the user tries to drop an icon anywhere other than on top of the canvas_mc movie clip instance, no duplicate will be created, and the icon will simply snap back to its original position below the canvas.

This variable is used to store the number of icons duplicated onto the canvas as well as to assign a unique depth to a duplicated icon and give it a unique name. iconDepth was used for these purposes in Step 4. It's defined outside of a function because it needs to exist for as long as the application is running.Choose Control > Test Movie to test your work. Drag the icons onto the canvas.If you drag an icon and drop it anywhere other than on the canvas_mc instance, it will return to its original location, unduplicated. If you release the icon over the canvas_mc movie clip instance, a duplicate will be created and the original will be sent back to its starting position.Close the test movie and save your work as draw4.fla.You've now created a simple, medium-sized drawing application. By applying the concepts you've learned here, you can create an application that includes many more features than this one.
var iconDepth:Number = 0;