Hi, I have built this, drag the various dots. http://gasolicious.com/flashstuff/kite%20drag%20offset%208_%20parent.htm
I did not use the flash startDrag because it had all sorts of issues.
I wanted the dots to be restricted to the stage when dragged.
That is any dot CAN leave the stage when it is not the one being dragged
For example make the “kite” big and drag the red dot, the blue dots are free to leave the stage (marked by outline in the online example)
The code I have uses mousemove, and some globalToLocal conversion to keep the dots from leaving the stage area
All was well so far.
Now I find out the “kite” has to be able to rotate!
when I simply rotate the kite all of my dragging rules break down.
Click the big blue button, each click rotates the kite 5 degrees. Looks OK at first, rotate 30 or 40 degrees and everything breaks dow
I know its the globalToLocal limiting, is there a better way?.
Does anyone see a way to fix what happens, or a whole new way to do the drag.
I am desperate ;-(
Flash file attached and dragging section of code below
var item:MovieClip;
var dragOffsetX:int;
var dragOffsetY:int;
var itemOffsetXY:int;
var stageW:int=800;
var stageH:int=650;
var dotHalf:Number=kite_mc.dotA_mc.width/2;
var offSetX:int;
var offSetY:int;
//basic blue rotate button for testing
blue_btn.addEventListener(MouseEvent.CLICK, rotateIt);
function rotateIt(e:MouseEvent):void {
kite_mc.rotation=kite_mc.rotation+5;
}
//add event listeners to all dots
kite_mc.dotA_mc.addEventListener(MouseEvent.MOUSE_DOWN, onItemDragStart);
kite_mc.dotB_mc.addEventListener(MouseEvent.MOUSE_DOWN, onItemDragStart);
kite_mc.dotC_mc.addEventListener(MouseEvent.MOUSE_DOWN, onItemDragStart);
kite_mc.dotD_mc.addEventListener(MouseEvent.MOUSE_DOWN, onItemDragStart);
kite_mc.dotF_mc.addEventListener(MouseEvent.MOUSE_DOWN, onItemDragStart);
function onItemDragStart( e:MouseEvent ):void {
item=e.target.parent as MovieClip;
dragOffsetX=item.x-item.parent.mouseX;//these 3 used to restrain dots to stage properly
dragOffsetY=item.y-item.parent.mouseY;
itemOffsetXY=item.width/2;
switch (item.name) {//call correct drag function depending on dot being dragged
case "dotA_mc" :
trace("case 1");
stage.addEventListener( MouseEvent.MOUSE_MOVE, dragItemA);
break;
case "dotB_mc" :
trace("Case 2");
stage.addEventListener( MouseEvent.MOUSE_MOVE, dragItemBD);
break;
case "dotC_mc" :
trace("Case 3");
stage.addEventListener( MouseEvent.MOUSE_MOVE, dragItemC);
break;
case "dotD_mc" :
trace("Case 4");
stage.addEventListener( MouseEvent.MOUSE_MOVE, dragItemBD);
break;
case "dotF_mc" :
trace("Case 5");
stage.addEventListener( MouseEvent.MOUSE_MOVE, dragItemF);
offSetX=stage.mouseX-kite_mc.x;
offSetY=stage.mouseY-kite_mc.y;
break;
}
//stop dragging on mouse up
stage.addEventListener( MouseEvent.MOUSE_UP, onItemDragStop );
}
//drag A mirror movement to C and x movement to F
function dragItemA(e:MouseEvent):void {
var topLeft:Point=item.parent.globalToLocal(new Point(0,0));
var bottomRight:Point=item.parent.globalToLocal(new Point(stageW,stageH));
item.x=Math.min(Math.max(item.parent.mouseX+dragOffsetX,topLeft.x+itemOffsetXY),bottomRight.x-itemOffsetXY);
item.y=Math.min(Math.max(item.parent.mouseY+dragOffsetY,topLeft.y+itemOffsetXY),bottomRight.y-itemOffsetXY);
kite_mc.dotC_mc.x=item.x;
kite_mc.dotC_mc.y=item.y*-1;
kite_mc.dotF_mc.x=item.x;
loop();
}
//drag B or D restrict movement to X plane
function dragItemBD(e:MouseEvent):void {
var topLeft:Point=item.parent.globalToLocal(new Point(0,0));
var bottomRight:Point=item.parent.globalToLocal(new Point(stageW,stageH));
item.x=Math.min(Math.max(item.parent.mouseX+dragOffsetX,topLeft.x+itemOffsetXY),bottomRight.x-itemOffsetXY);
loop();
}
//drag C mirror movement to A and x movement to F
function dragItemC(e:MouseEvent):void {
var topLeft:Point=item.parent.globalToLocal(new Point(0,0));
var bottomRight:Point=item.parent.globalToLocal(new Point(stageW,stageH));
item.x=Math.min(Math.max(item.parent.mouseX+dragOffsetX,topLeft.x+itemOffsetXY),bottomRight.x-itemOffsetXY);
item.y=Math.min(Math.max(item.parent.mouseY+dragOffsetY,topLeft.y+itemOffsetXY),bottomRight.y-itemOffsetXY);
kite_mc.dotA_mc.x=item.x;
kite_mc.dotA_mc.y=item.y*-1;
kite_mc.dotF_mc.x=item.x;
loop();
}
// drag complete kite but restrain only dot F to stage area
function dragItemF(e:MouseEvent):void {
kite_mc.y=stage.mouseY-offSetY;
kite_mc.x=stage.mouseX-offSetX;
var i5:Point=item.localToGlobal(new Point(0,0));
if (i5.y-dotHalf<0) {
kite_mc.y=0-item.y+dotHalf;
}
if (i5.y+dotHalf>stageH) {
kite_mc.y=stageH-item.y-dotHalf;
}
if (i5.x-dotHalf<0) {
kite_mc.x=0-item.x+dotHalf;
}
if (i5.x+dotHalf>stageW) {
kite_mc.x=stageW-item.x-dotHalf;
}
loop();
}
//draw kite one last time just in case fast mouse movement gets dot ahead of line
//remove all possible mouse move listeners
function onItemDragStop( e:MouseEvent ):void {
loop();
stage.removeEventListener( MouseEvent.MOUSE_MOVE, dragItemA );
stage.removeEventListener( MouseEvent.MOUSE_MOVE, dragItemBD );
stage.removeEventListener( MouseEvent.MOUSE_MOVE, dragItemC );
stage.removeEventListener( MouseEvent.MOUSE_MOVE, dragItemF );
stage.removeEventListener( MouseEvent.MOUSE_UP, onItemDragStop );
}