Scriptplayground Network

Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » as » Drag a MovieClip in ActionScript 3

Drag a MovieClip in ActionScript 3

How to setup a MovieClip so that it can be dragged using Event handlers in ActionScript 3.

Dragging a MovieClip is a very common task in a Flash application or movie. The code to achieve this result is fairly simple

For this example, the dragging code will be assigned within a function for portability

function initDragger(mc:MovieClip):void
{	
	...	
}

Here is the contents of that function which is setup using event listeners. The MouseEvent Class has a custom function attached where the drag code is called.

mc.addEventListener(MouseEvent.MOUSE_DOWN, function (e:MouseEvent):void
{
	e.currentTarget.startDrag();
});
mc.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void
{
	e.currentTarget.stopDrag();
});

The final step is to make a call to the newly created function and test it all out

function initDragger(mc:MovieClip):void
{	
	mc.addEventListener(MouseEvent.MOUSE_DOWN, function (e:MouseEvent):void
	{
		e.currentTarget.startDrag();
	});
	mc.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void
	{
		e.currentTarget.stopDrag();
	});
	
}

// Set up drag
initDragger(boxMC);

Update: After this tutorial was finished it was brought to my attention that inline functions can cause a memory leak over time. This is obviously not the desired result, so here is the same code modified to prevent that memory leak.


function initDragger(mc:MovieClip):void
{
	mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
	mc.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}

function mouseDownHandler(e:MouseEvent):void
{
	e.currentTarget.startDrag();
}
function mouseUpHandler(e:MouseEvent):void
{
	e.currentTarget.stopDrag();
}

// Set up drag
initDragger(boxMC);
| Print It |  Follow Scriptplayground on Twitter (@scriptplay)

Comments: Drag a MovieClip in ActionScript 3

 jacob  Sat Dec 1, 2007 8:36 am  
doing in line functions for event handlers is a sure fire way to have an awesome mem leak in as3. they shouldn't be declared that way because there is no way to remove them.
 me  Fri Dec 14, 2007 5:57 am  
cool thanks
 owen  Mon Mar 17, 2008 6:13 pm  
@jacob
So... how do you accomplish the same without the memory leak?
 mkeefe  Mon Mar 17, 2008 6:51 pm  
@owen,

I have updated the code, this previous version was provided a while ago.

Thanks for reminding me :)
 Damilola Jegede  Mon Mar 23, 2009 2:44 am  
Thanks. God bless you!
 rouslan  Wed Jul 22, 2009 4:54 am  
Hi, any ideas why this doesn't work:

function initDragger(mc:MovieClip):void
{
mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
mc.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}

function mouseDownHandler(e:MouseEvent):void
{
e.currentTarget.startDrag();
}
function mouseUpHandler(e:MouseEvent):void
{
e.currentTarget.stopDrag();
}

// Set up drag
//initDragger(grafiks);





//getting the mouse click report
stage.addEventListener(MouseEvent.CLICK, reportClick);

function reportClick(event:MouseEvent):void
{
initDragger(event.target.name);
}
 Kamran  Fri Oct 9, 2009 2:17 am  
Ah thanks, that helped a lot! ;)
 Alvin  Thu Dec 10, 2009 5:54 am  
// Set up drag
initDragger(boxMC);

shouldnt this refer to the mc?

i.e

initDragger(mc);
 Ronie Neubauer  Wed Feb 24, 2010 3:23 pm  
Thanks!
Add a comment
Name:
Website:
Comment:
Please note: Offensive comments, flaming and spamming is not permitted on this site and your comment will be deleted immediately.

HTML is not allowed.

Please provide all comments in English so that others can help you. A common helper in this is to use an online translator.

As a security measure your ip will be recorded.
 
Anti-Robot Check:

Enter the key you see above.

What is this?: This extra test has been added due to the recent explosion of spam.
 
Google