# Confining mouse actions to MC on stage

**URL:** <https://forum.kirupa.com/t/confining-mouse-actions-to-mc-on-stage/484732>\
**Category:** flash\
**Created:** [January 21, 2015, 5:18pm UTC](https://forum.kirupa.com/t/confining-mouse-actions-to-mc-on-stage/484732 "2015-01-21T17:18:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsimpsonJSD](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@jsimpsonJSD](https://forum.kirupa.com/u/jsimpsonJSD)\
**Post date:** [January 21, 2015, 5:18pm UTC](https://forum.kirupa.com/t/confining-mouse-actions-to-mc-on-stage/484732/1 "2015-01-21T17:18:53Z")

</div>

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);  
}

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [January 21, 2015, 7:52pm UTC](https://forum.kirupa.com/t/confining-mouse-actions-to-mc-on-stage/484732/2 "2015-01-21T19:52:40Z")

</div>

You could put your MOUSE\_DOWN/MOUSE\_MOVE handlers on mc\_boundingBox

---

<div class="post-metadata">

**Author:** ![jsimpsonJSD](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@jsimpsonJSD](https://forum.kirupa.com/u/jsimpsonJSD)\
**Post date:** [January 21, 2015, 8:28pm UTC](https://forum.kirupa.com/t/confining-mouse-actions-to-mc-on-stage/484732/3 "2015-01-21T20:28:33Z")

</div>

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!! 😄
