<classwork>

Drag and Drop


In order to drag something on the stage it needs to be both a button and a movie clip. Buttons can be selected and clicked on and movie clips can be moved with programming. So, it needs to be both. Who knows if this will always be the case, as buttons can now have instance names in Flash MX 2004.

But the easiest way to drag and drop is to make a button out of what you want to drag and attach the following programming to it:

on (press){
this.startDrag();
}

on (release){
this.stopDrag();
}

Then convert the button into a Movie Clip (select it and press F8 or convert to symbol).

note: Converting the button symbol into a movie clip appears to be optional for MX. It may be that it is no longer necessary.

You can also now attach the same code directly to a movie clip symbol:

1. Make your movie clip. Select it. Open the actions panel and enter:

on (press){
this.startDrag();
}

on (release){
this.stopDrag();
}

Parameters

Dragging with constraint

my_mc.startDrag([lock,[left, top, right, bottom]])

lock:
A Boolean value specifying whether the draggable movie clip is locked to the center of the mouse position (true), or locked to the point where the user first clicked on the movie clip (false). This parameter is optional.

left, top, right, bottom: Values relative to the coordinates of the movie clip's parent that specify a constraint rectangle for the movie clip. These parameters are optional.

ex.
this.startDrag([true,[-50, 100, 50, -100]])

download the drag tutorial (file name: 10drag.fla)
right click on the file (control-click for Mac) and select to download to your drive.


You can also now use an easy changeable Function to drag and drop.
ex.

1. Make a movie clip and give it an instance name, say "mymovie"

2. On frame 1 of your main Timeline make the following action:

mymovie.onPress =function(){
this.startDrag();
}

mymovie.onRelease =function(){
this.stopDrag();
}


See Collision Detection on the previous page for extending drag and drop to drop and lock on a specific target.