Flash AS3 Tutorials Center | Interesting Flash Animation Tutorials | Valuing Flash Animation Tutorials | Flash ActionScript and Animation Home

AS3 Beginner Tutorials | AS3 Basic Lessons | AS3 Valuing Courses | AS3 Components Tutorials | AS3 and PHP Interaction Tutorials
AS3 Practical Tutorials | AS3 Animation Techniques | AS3 Transition Effects Tutorials | AS3 Download Upload Files | AS3 Particle Systems
Communication Between Flash Movies with AS3 | AS3 and JavaScript interaction | AS3 Matrix Transformation | AS3 Physics Simulation Tutorials

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.

Please update flash player to view 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.