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.
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.
Here is a code to hold clip in ellipse area closed in rect
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;
}
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.
:: Copyright KIRUPA 2024 //--