AddThis Social Bookmark Button
 Advanced drag and drop in Flash (Flash MX, MX 2004, Flash 8)

download source files
download source files

Drag and drop activities are very popular in Flash, it is commonly use to create learning activities where elements needs to be associated. It is not difficult to create a drag and drop in Flash. We will first have a look at the two methods startDrag and stopDrag; we will then move on some more advanced tips and tricks to extend the radius of our knowledge on the subject, Let's jump into the action...

Tutorial part 1 : The basics

1. create a new FLA

2. Create a basic 50x50 red circle (fig1). Press F8, name the symbol redCircleMC and convert it as a movie clip (fig2). Be sure to set the registration point at the center (fig3), you will understand why you need to do that later on. In the panel properties, give it the Instance name redCircle (fig4).

red circle
fig1 convert to movie clip fig2 registration point fig3 instance name fig4

Now let's assign a bit of code to that movie clip to be able to drag and drop it anywhere on the stage.

3. on the first frame of the main timeline enter the following code :

redCircle.onPress = function(){
startDrag(this,true)
}

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

In simple words when the red circle is pressed (onPress action) the drag action occurs.
When the mouse is released (onRelease action) the drag ends.

let's have a quick look at the startDrag parameters :

This is what the Flash 8 manual tells us about the startDrag method :

startDrag function

startDrag
(target:Object, [lock:Boolean, left:Number, top:Number, right:Number, bottom:Number])

Makes the target movie clip draggable while the movie plays. Only one movie clip can be dragged at a time. After a startDrag() operation is executed, the movie clip remains draggable until it is explicitly stopped by stopDrag() or until a startDrag() action for another movie clip is called.

Parameters

target:Object - The target path of the movie clip to drag.

lock:Boolean [optional] - 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 the movie clip (false ).

left,top,right,bottom:Number [optional] - Values relative to the coordinates of the movie clip's parent that specify a constraint rectangle for the movie clip.

The first parameter target is easy to understand, it represents the movie clip that you want to drag. In our code above we use the keyword this to refer to the movie clip redCircle but we might as well replace this by redCircle it would do the job as well in our case. In any case the first parameter target need to refer to the Movie clip that you want to drag.


Continue to next page...
AddThis Social Bookmark Button
If you think this page is providing useful information, don't hesitate to leave a comment.

[ 1 comment ]

flashvalley
 
Copyright ©2006-2008 flashvalley.com - All rights reserved