Newsletter: Learn More ||| Multimedia Presentation Software |||
Drag and Drop in Flash MX Tutorial
Introduction
Here is a drag and drop tutorial for Flash MX.

The finished movie should look like this:
Download the source file for this tutorial here.
Instructions
Start by opening Flash. Make a new file and set the Frame rate to 21. Draw a circle. Select the circle and hit F8 to convert it to a Movie Clip (MC) - name it circle_mc.
Also give the circle_mc on the stage an instance name of circle_mc.
Make a new Layer and name it "actions"

On frame 1 of the actions layer put this -

circle_mc.onPress=function(){
startDrag(this);
}
circle_mc.onRelease=circle_mc.onReleaseOutside=function()
{
stopDrag();
}

Now save your movie as dragndrop.fla and go Control/Test Movie.

You should be able to drag your cicle around - if you can't then you've done something wrong.

Underneath the action actions you already have on frame 1 add this:

//the variables below will store the clips starting position
circle_mc.myHomeX=circle_mc._x;
circle_mc.myHomeY=circle_mc._y;

circle_mc.onMouseDown=function(){
//this variable tells us if the mouse is up or down
mousePressed=true;
}
circle_mc.onMouseUp=function()
{
mousePressed=false;
}
circle_mc.onEnterFrame=function()
{
//all these actions basically just say "if the mouse is up (in other words - the clip is not being dragged)
// then move the MC back to its original starting point (with a smooth motion)"
if(mousePressed==false)
{
this._x-=(this._x-this.myHomeX)/5;
this._y-=(this._y-this.myHomeY)/5;
}
}

Paste this into flash and then have a read of the code comments.

Test your movie again and you will see that you can drag your circle around but when you let go of the mouse the circle moves back to its starting position with a smooth motion.

Now we want to check where the circle is bring dropped. Start by creating a new layer and dragging the new layer below the layer that has the circle_mc on it. Now draw a big circle down the bottom right hand corner of the stage. Select the circle you have just drawn and convert it to a movie clip - name it target circle and give it an instance name of targetCircle. Double click on the target MC to go inside it. Put a stop action on both first and second frames. Create a new keyframe for the circle in frame 2 (hit F6) and give it a different fill colour.

Now go back to the main timeline and onRelease actions to this:

circle_mc.onRelease=circle_mc.onReleaseOutside=function(){
stopDrag();
if (this._droptarget == "/targetCircle")
{
this.onTarget=true;
_root.targetCircle.gotoAndStop(2);
}else{
this.onTarget=false;
_root.targetCircle.gotoAndStop(1)
}
}

and the if statement in the enterFrame actions to this

if(mousePressed==false&&this.onTarget==false){

Still with me? Test the movie again and watch what happens when you drag the circle around - both on and off the target.

What else? Perhaps when we drag the circle on to any part of the target, we want the circle to move to the center of the target. Let's do that.

Change all the actions on frame 1 to this:

circle_mc.onPress = function() {
startDrag(this);
};
circle_mc.onRelease = circle_mc.onReleaseOutside=function ()
{
stopDrag();
if (this._droptarget == "/targetCircle")
{
this.onTarget = true;
_root.targetCircle.gotoAndStop(2);
} else {
this.onTarget = false;
_root.targetCircle.gotoAndStop(1);
}
};
//the variables below will store the clips starting position
circle_mc.myHomeX=circle_mc._x;
circle_mc.myHomeY=circle_mc._y;
//the variables below will store the clips end position
circle_mc.myFinalX = 443;
circle_mc.myFinalY = 294;
circle_mc.onMouseDown = function()
{
//this variable tells us if the mouse is up or down
mousePressed = true;
};
circle_mc.onMouseUp = function()
{
mousePressed = false;
};
circle_mc.onEnterFrame = function()
{
//all these actions basically just say "if the mouse is up (in other words - the clip is not being dragged)
// then move the MC back to its original starting point (with a smooth motion)"
if (mousePressed == false && this.onTarget == false)
{
this._x -= (this._x-this.myHomeX)/5;
this._y -= (this._y-this.myHomeY)/5;
//if the circle is dropped on any part of the target it slides to the center of the target
} else if (mousePressed == false && this.onTarget == true) {
this._x -= (this._x-this.myFinalX)/5;
this._y -= (this._y-this.myFinalY)/5;
}
};

The value of the myFinalX and myFinalY variables will vary depending on the position of your target. Just move dragable circle to the center of the target and make a note of it's x and y position in the info panel and change the value given to those variables accordingly. Then move the circle back to it's starting position.

Test the movie again and watch what happens (or look away and cross your fingers - it's up to you...).

This tutorial was written by Bill Trikojus of Tableau Design.
 
©2007 Wildform, Inc | Policies | Contact Us | Newsletter Options
 
Wildform provides a 100% satisfaction guarantee on all our software. If you are not completely satisfied with our software for any reason you may request a refund within 15 days of purchase.