# Confining mouse actions to MC on stage, v2

**URL:** <https://forum.kirupa.com/t/confining-mouse-actions-to-mc-on-stage-v2/505113>\
**Category:** flash\
**Created:** [January 30, 2015, 2:15am UTC](https://forum.kirupa.com/t/confining-mouse-actions-to-mc-on-stage-v2/505113 "2015-01-30T02:15:04Z")\
**Posts on this page:** 4\
**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 30, 2015, 2:15am UTC](https://forum.kirupa.com/t/confining-mouse-actions-to-mc-on-stage-v2/505113/1 "2015-01-30T02:15:05Z")

</div>

Two part question, please bear with me.

Q1. Trying a different method than before to draw straight lines between mouse clicks, confined WITHIN a movie clip on the stage whose instance named “mc\_border”. I have a separate mc adjacent to mc\_border which acts as a simple color picker, setting a var “currentColor”…which is why I don’t want this drawing routine to use the entire stage. Here’s the code:

* * *

```
var x1:Number=NaN;
var y1:Number=NaN;
var lines:Array=new Array;
stage.addEventListener(MouseEvent.CLICK,setStart);

function setStart(e:MouseEvent):void {
lines.push(new Sprite);
stage.addChild(lines[lines.length-1]);
x1=mouseX;
y1=mouseY;
stage.removeEventListener(MouseEvent.CLICK,setStart);
stage.addEventListener(MouseEvent.MOUSE_MOVE,drawLine);
stage.addEventListener(MouseEvent.CLICK,setEnd);
}

function drawLine(e:MouseEvent):void {
lines[lines.length-1].graphics.clear();
lines[lines.length-1].graphics.lineStyle(2,currentColor);
lines[lines.length-1].graphics.moveTo(x1,y1);
lines[lines.length-1].graphics.lineTo(mouseX,mouseY);
}

function setEnd(e:MouseEvent):void {
	stage.removeEventListener(MouseEvent.CLICK,setEnd) ;
	stage.removeEventListener(MouseEvent.MOUSE_MOVE,drawLine);
	stage.addEventListener(MouseEvent.CLICK,setStart);
}

```

This works to the point where I can draw my lines (one click to start, second click to stop) anywhere on the stage, including the color picker mc. I’ve made the following changes to the mouse events that make sense to me, but it does not work…

```
var x1:Number=NaN;
var y1:Number=NaN;
var lines:Array=new Array;
mc_border.addEventListener(MouseEvent.CLICK,setStart);

function setStart(e:MouseEvent):void {
lines.push(new Sprite);
stage.addChild(lines[lines.length-1]);
x1=mouseX;
y1=mouseY;
mc_border.removeEventListener(MouseEvent.CLICK,setStart);
stage.addEventListener(MouseEvent.MOUSE_MOVE,drawLine);
mc_border.addEventListener(MouseEvent.CLICK,setEnd);
}

function drawLine(e:MouseEvent):void {
lines[lines.length-1].graphics.clear();
lines[lines.length-1].graphics.lineStyle(2,currentColor);
lines[lines.length-1].graphics.moveTo(x1,y1);
lines[lines.length-1].graphics.lineTo(mouseX,mouseY);
}

function setEnd(e:MouseEvent):void {
	mc_border.removeEventListener(MouseEvent.CLICK,setEnd) ;
	stage.removeEventListener(MouseEvent.MOUSE_MOVE,drawLine);
	mc_border.addEventListener(MouseEvent.CLICK,setStart);
}

```

What am I missing here?

Q2: The mc\_border clip also contains simple buttons which get very glitchy when rolled over while trying to draw the lines. This project is a simple “wiring simulator” in which colored wires are drawn between pins on two connectors. “mc\_border” contains the pin diagram drawings for the connectors, I plan to have active hotspots watching for mouse clicks… Is “stacking interactivity” even possible, or should I try a different approach?

Sincere thanks for any suggestions!!

---

<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 30, 2015, 2:40am UTC](https://forum.kirupa.com/t/confining-mouse-actions-to-mc-on-stage-v2/505113/2 "2015-01-30T02:40:01Z")

</div>

Is the problem your line draws outside of mc\_border after the first click? Don’t you want MOVE on mc\_border?

The glitchiness of your buttons is probably because you’re creating sprites with graphics that is being drawn under the mouse for your lines. You can probably fix this by setting mouseEnabled = false for each of those sprites.

---

<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 30, 2015, 3:31am UTC](https://forum.kirupa.com/t/confining-mouse-actions-to-mc-on-stage-v2/505113/3 "2015-01-30T03:31:17Z")

</div>

Thanks for the quick reply! Yes, that was the initial problem…that if I click a color button on the palette first (outside of mc\_border) it starts drawing on THAT mouse click, outside the intended drawing area. Basically I want confine all “line-drawing mouse actions” to be within mc\_border, allowing other mc’s elsewhere on the stage to retain their own mouse functions.

---

<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 30, 2015, 8:13am UTC](https://forum.kirupa.com/t/confining-mouse-actions-to-mc-on-stage-v2/505113/4 "2015-01-30T08:13:38Z")

</div>

is that, or is that not fixed by changing stage to mc\_border for each of the events?
