Confining mouse actions to MC on stage

Hey gang - newbie with AS3 here. What I’m doing here is drawing a simple straight line on stage by clicking and dragging. This code works fine, but I only want this to happen within a specified area of the stage. How can I apply this code in such a way that it only executes if the cursor is over (thereby within the boundaries of) say a rectangle with an instance name of “mc_boundingBox”? Thanks in advance for any assistance.


var line:Sprite;
var xloc;
var yloc;
line = new Sprite();
addChild(line);

stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);

function mouseDownHandler(event:MouseEvent):void {
trace (“mouse down”);
xloc = mouseX;
yloc = mouseY;
line.graphics.lineStyle(2);

line.graphics.moveTo(xloc,yloc);
stage.addEventListener(MouseEvent.MOUSE_MOVE,movement);

}
function mouseUpHandler(event:MouseEvent):void {
trace (“mouse up”);
line.graphics.clear();
stage.removeEventListener(MouseEvent.MOUSE_MOVE,movement);
}
function movement(event:MouseEvent):void {
trace (“mouse moving”);
line.graphics.clear();
line.graphics.lineStyle(2);
line.graphics.moveTo(xloc,yloc);
line.graphics.lineTo(mouseX,mouseY);
}

You could put your MOUSE_DOWN/MOUSE_MOVE handlers on mc_boundingBox

Thanks for the reply! I also changed:

function mouseUpHandler(event:MouseEvent):void {
trace (“mouse up”);
line.graphics.clear();
mc_border.removeEventListener(MouseEvent.MOUSE_MOVE,movement);
}

Works great now, thank you!! :smile: