Home | Contact Us | Store

Mouse Drag in Limited Area with Flash ActionScript 3

In most Flash Movies, you need objects to be dragged by mouse within a boundary. With Flash ActionScript 3, this is quite easy to drag MovieClips in a limited boundary on the stage.
Your flash player version is too old to see this Flash ActionScript tutorial!

Flash Tutorial Content:

Many Flash application and games require using mouse to drag some objects on the stage. In most cases we usually want the objects to be dragged within or inside a limited boundary. With Flash ActionScript 3, this is quite easy to accomplish this effect.

The complete Flash Movie is shown as above, you may try how it works before you start this tutorial.

Flash ActionScript Codes:

// Control dragging to a specific area of your movie.
// Specify 4 values
// (left, top, right - distance from left, bottom - distance from top)
var rectangle:Rectangle = new Rectangle(100, 100, 200, 150);

 

stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseStartDrag);

 

function mouseStartDrag(motion:MouseEvent):void {

// Start dragging when square_mc is pressed
square_mc.startDrag(false, rectangle);

// Dispaly message
output_txt.text = "Dragging Square now....";

}

stage.addEventListener(MouseEvent.MOUSE_UP, mouseStopDrag);

 

function mouseStopDrag(motion:MouseEvent):void {

// Stop dragging when square_mc is released
square_mc.stopDrag();

// Display message
output_txt.text = "Square was dragged to: " + "(" + square_mc.x + ", " + square_mc.y + ")";

}

Download Flash Source File:

Flash Source File mouse-5.fla

Remarks:

This Flash ActionScript tutorial shows how to drag objects or movieclip within a boundary area.

Mouse Control Flash ActionScript Tutorial