# Constrain MovieClip to irregular area

**URL:** https://forum.kirupa.com/t/constrain-movieclip-to-irregular-area/261942
**Category:** flash
**Created:** [June 2, 2008, 2:17am UTC](https://forum.kirupa.com/t/constrain-movieclip-to-irregular-area/261942 "2008-06-02T02:17:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Matt\_RKLS](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@Matt\_RKLS](https://forum.kirupa.com/u/Matt_RKLS)
#### Post date: [June 2, 2008, 2:17am UTC](https://forum.kirupa.com/t/constrain-movieclip-to-irregular-area/261942/1 "2008-06-02T02:17:33Z")

</div>

Hello!

I am looking to constrain a draggable movieclip within a non-rectangular (pie shaped) area… using AS3. I can drag out the old trig books, but thought some Flash whiz may have a shortcut approach to get me started. Any ideas? :snug: Thanks.

---

<div class="post-metadata">

### Author: ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)
#### Post date: [June 3, 2008, 12:16pm UTC](https://forum.kirupa.com/t/constrain-movieclip-to-irregular-area/261942/2 "2008-06-03T12:16:51Z")

</div>

Here is a code to hold clip in ellipse area closed in rect

```auto
drag_MC.addEventListener(MouseEvent.MOUSE_DOWN,startDragging);
//--------CONFIG--------
var rectangleRotation:Number=0;//only use when transformations are applied such as rotation
var rectangle:Rectangle=new Rectangle(100,100,400,200);
//-------GRAPHICS-------
graphics.beginFill(0xFFFFFF);
graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
graphics.endFill();
graphics.lineStyle(1,0);
graphics.drawRect(100,100,400,200);
//-THANKS-TO-WIKIPEDIA--
var beta:Number = -rectangleRotation / 180 * Math.PI;
var sinbeta:Number = Math.sin(beta);
var cosbeta:Number = Math.cos(beta);
var draggedClip:DisplayObject;
function startDraging(event:MouseEvent):void {
 draggedClip=DisplayObject(event.currentTarget);
 stage.addEventListener(MouseEvent.MOUSE_MOVE,drag);
 stage.addEventListener(MouseEvent.MOUSE_UP,stopDragging);
}
function stopDragging(event:MouseEvent):void {
 event.currentTarget.removeEventListener(MouseEvent.MOUSE_UP,stopDragging);
 event.currentTarget.removeEventListener(MouseEvent.MOUSE_MOVE,drag);
}
function drag(event:MouseEvent):void {
 var X:Number=(rectangle.left + rectangle.right) / 2;
 var Y:Number=(rectangle.top + rectangle.bottom) / 2;
 var angle:Number=Math.atan2(mouseY - Y,mouseX - X);
    var sinalpha:Number=Math.sin(angle);
    var cosalpha:Number=Math.cos(angle);
 var dX:Number=X + (rectangle.width / 2 * cosalpha * cosbeta - rectangle.height / 2 * sinalpha * sinbeta);
 var dY:Number=Y + (rectangle.width / 2 * cosalpha * sinbeta + rectangle.height / 2 * sinalpha * cosbeta);
 if (dX - X>0) {
  if(mouseX<dX) dX=mouseX;
 } else {
  if(mouseX>dX) dX=mouseX;
 }
 if (dY - Y<0) {
  if(mouseY>dY) dY=mouseY;
 } else {
  if(mouseY<dY) dY=mouseY;
 }
 draggedClip.x=dX;
 draggedClip.y=dY;
}

```

---

<div class="post-metadata">

### Author: ![Fidodo](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@Fidodo](https://forum.kirupa.com/u/Fidodo)
#### Post date: [June 3, 2008, 8:15pm UTC](https://forum.kirupa.com/t/constrain-movieclip-to-irregular-area/261942/3 "2008-06-03T20:15:47Z")

</div>

I was thinking, just make a movieclip of the shape you want in the FLV, link it as a MovieClip. Then use a point to movieclip hit test where the point is the center of the constrained movie clip. Then it would be confined to that area perfectly.
