In this tutorial, let us create a smooth drag and drop functionality using Tweener Class.
Download Source Files




import caurina.transitions.*;
var cirX:Number = 60;
var cirY:Number = 60;
drag_mc.addEventListener(MouseEvent.MOUSE_DOWN,
beginDrag);
function beginDrag(e:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveMC);
stage.addEventListener(MouseEvent.MOUSE_UP,endDrag);
}
function endDrag(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveMC);
drag_mc.stopDrag();
if (drag_mc.hitTestObject(target_mc)) {
Tweener.addTween(drag_mc,{x:340,
y:145,
time:1,
transition:"easeIn"});
} else {
Tweener.addTween(drag_mc,{x:60,
y:60,
time:1,
transition:"easeIn"});
}
stage.removeEventListener(MouseEvent.MOUSE_UP,endDrag);
}
function moveMC(e:MouseEvent):void {
Tweener.addTween(drag_mc,{x:mouseX,y:mouseY,time:0.5,transition:"easIn"});
e.updateAfterEvent();
}
import caurina.transitions.*;
var cirX:Number = 60;
var cirY:Number = 60;
drag_mc.addEventListener(MouseEvent.MOUSE_DOWN,
beginDrag);
function beginDrag(e:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveMC);
stage.addEventListener(MouseEvent.MOUSE_UP,endDrag);
}
function moveMC(e:MouseEvent):void {
Tweener.addTween(drag_mc,{x:mouseX,y:mouseY,time:0.5,transition:"easIn"});
e.updateAfterEvent();
}
function endDrag(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveMC);
if (drag_mc.hitTestObject(target_mc)) {
Tweener.addTween(drag_mc,{x:340,
y:145,
time:1,
transition:"easeIn"});
} else {
Tweener.addTween(drag_mc,{x:60,
y:60,
time:1,
transition:"easeIn"});
}
stage.removeEventListener(MouseEvent.MOUSE_UP,endDrag);
}
Labels: ActionScript 3, Flash
Tutorial is great, but I'm having problem with add similar function to another movie clip. Can You add code for example two MC's? Thanks a lot for help.
By Remi, at December 16, 2008 1:15 PM
I have the same problem of REMI,
"I'm having problem with add similar function to another movie clip." i need it for 3 MC can you send me the code you send remi please??
By dippa31, at January 5, 2009 8:57 AM
could I please also have the code example for multiple mcs?
By nsolcis, at January 19, 2009 12:07 AM
For multiple mc's create a custom class and asign it to each MovieClip via the "linkage" property. If you substitute "this" with the instance name, the code should work with any mc.
//Here's another solution.
//create two MovieClips titled
//pane1
//pane2
//paste following code
//note using TweenLite here
import gs.TweenLite;
import fl.motion.easing.*;
pane.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
pane.addEventListener(MouseEvent.MOUSE_UP, endDrag);
pane2.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
pane2.addEventListener(MouseEvent.MOUSE_UP, endDrag);
function beginDrag(e:MouseEvent):void
{
e.target.addEventListener(MouseEvent.MOUSE_MOVE, smoothDrag);
}
function smoothDrag(e:MouseEvent):void
{
TweenLite.to(e.target, .5, {x:mouseX, y:mouseY, ease:Cubic.easeOut});
}
function endDrag(e:MouseEvent):void
{
e.target.removeEventListener(MouseEvent.MOUSE_MOVE, smoothDrag);
}
//e.target used for the instance //name will target any
//mc with
//the event listener
//custom class save as SmoothSDrag.as
package
{
import gs.TweenLite;
import fl.motion.easing.*;
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Stage;
public class SmoothDrag extends MovieClip
{
var sd:Boolean;
public function SmoothDrag()
{
}
public function startSmoothDrag(mc:MovieClip, sd:Boolean, w:Number)
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, beginDrag)
function beginDrag(e:MouseEvent):void
{
if(sd == true)
{
TweenLite.to(mc, w, {x:mouseX, y:mouseY, ease:Cubic.easeOut});
}else{
TweenLite.to(mc, w, {x:mc.x, y:mc.y, ease:Cubic.easeOut});
}
}
}
}
}
//then use the class in your .fla like this
import gs.TweenLite;
import fl.motion.easing.*;
import SmoothDrag;
var sd:SmoothDrag = new SmoothDrag();
addChild(sd)
pane3.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
function beginDrag(e:MouseEvent):void
{
sd.startSmoothDrag(pane3, true, .5);
}
stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);
function endDrag(e:MouseEvent):void
{
sd.startSmoothDrag(pane3, false, .5);
}
//not perfect, I'm a noob, but gets //the job done.
Cool, I've been trying to modify the code so that you can drag along a large movieclip (i.e. a thumbmail panel), but I haven't had success in getting it to work.
Does anyone have any ideas?
By , at November 5, 2009 1:26 AM