2010-06-03 2 views
1

Flash에서 ActionScript3를 사용하여 커서를 따라 가면서 다른 MovieClip의 불규칙한 모양에 제약을받는 MovieClip을 만들 수 있습니까?FLASH/AS3 : 커서를 따라 가면서 불규칙한 영역에 구속 된 그림

편집 :

stage.addEventListener(MouseEvent.MOUSE_MOVE,follow); 
function follow(evt:MouseEvent){ 
     if(container.hitTestPoint(mouseX, mouseY, true)) { 
    cursor.x = mouseX; 
    cursor.y = mouseY; 
     } else { 
     var dx:int = cursor.mouseX; 
     var dy:int = ; 
    cursor.x = dx; 
    cursor.y = cursor.mouseY; 

     } 
} 

는 내가 달성하고자하는 컨테이너 MC를 벗어나면 커서 MC는 여전히 커서를 "따라"있는지 확인하는 것입니다,하지만 벗어날 수 없다 : 이것은 내가 필요한 정도입니다 그것에서.

그렇게 오래된 AS2 스크립트를,하지만 난 그것을 변환하는 방법을 잘 모르겠어요 :

onClipEvent (mouseMove) { 
    tX = _parent._xmouse; 
    // tX/tY are 'target' X/Y. 
    tY = _parent._ymouse; 
    if (_parent.constraintzone.hittest(tX, tY, true)) { 
     _x = tX; 
     _y = tY; 
    } else { 
     // and now the hurting begins 
     // get XY of center of constraint zone 
     cX = _parent.constraintzone._x; 
     // cX/cY are 'constrained' X/Y, 
     cY = _parent.constraintzone._y; 
     // found somewhere inside the constraint zone. 
     accuracy = 1; 
     // smaller = more accurate. 
     do { 
      dX = (tX-cX)/2; 
      // dX/dY are deltas to the midpoint between 
      dY = (tY-cY)/2; 
      // target XY and constrained XY. 
      if (_parent.constraintzone.hittest((tX-dX), (tY-dY), true)) { 
       cX += dX; 
       // midpoint is in; step out towards mouse. 
       cY += dY; 
      } else { 
       tX -= dX; 
       // midpoint is out; step in towards center. 
       tY -= dY; 
      } 
      // loop end. 
      // (dD > .5) is more accurate, (dX > 10) is less. 
     } while ((Math.abs(dX)>accuracy) || (Math.abs(dY)>accuracy)); 
     _x = tX; 
     // we're done, set the final position. 
     _y = tY; 
    } 
} 

답변

1

는 3과 같은 것을에서 보일 것이다 붙여 넣은 코드 :

stage.addEventListener(MouseEvent.MOUSE_MOVE,follow); 

function follow(evt:MouseEvent) { 
if (container.hitTestPoint(mouseX, mouseY, true)) { 
    cursor.x = mouseX; 
    cursor.y = mouseY; 
} else { 
    var cX:Number = container.x + (container.width/2); 
     // cX/cY are 'constrained' X/Y, 
     var cY:Number = container.y + (container.height/2); 
     // found somewhere inside the constraint zone. 
    var tX:Number = mouseX; 
    var tY:Number = mouseY; 

    var accuracy:Number = 1; 
     // smaller = more accurate. 
     do { 
      var dX:Number = (tX-cX)/2; 
      // dX/dY are deltas to the midpoint between 
      var dY:Number = (tY-cY)/2; 
      // target XY and constrained XY. 
      if (container.hitTestPoint((tX-dX), (tY-dY), true)) { 
       cX += dX; 
       // midpoint is in; step out towards mouse. 
       cY += dY; 
      } else { 
       tX -= dX; 
       // midpoint is out; step in towards center. 
       tY -= dY; 
      } 
      // loop end. 
      // (dD > .5) is more accurate, (dX > 10) is less. 
     } while ((Math.abs(dX)>accuracy) || (Math.abs(dY)>accuracy)); 
     cursor.x = tX; 
     // we're done, set the final position. 
     cursor.y = tY; 
} 
} 

그것은이다 시원하고 멋진 것은 아니지만 합리적으로 빠르다. 그래서, 나는 당신의 실제 모양으로 그것을 검사 할 것입니다. 그것은 충분히 좋을지도 모릅니다.

+0

작동하지만 컨테이너 (container.x + container.width)/2에 대해 container.x + (container.width/2)를 변경했습니다. 이러한 개체가 두 개있는 방법이 있는지 알고 계십니까? 나는 MC의 사본과 다른 변수를 가진 스크립트의 복사본을 만들려고 노력했지만 어떤 이유로 든 나는 오른쪽 엉망진창에 넣었다. – peroyomas

+0

다시 container.x + container.width/4로 변경하고 모든 경우에 잘 작동합니다. – peroyomas

0

마우스의 위치를 ​​찾기 위해 다음 mousex의 쥐의를 사용할 수 있습니다 그러나 hitTest 또는 롤오버 출시 이벤트를 사용하는 경우 . 마우스에 가장 가까운 점이 필요한 경우에는 시행 착오의 멋진 세상에 있습니다. 만나다;

stackoverflow.com/questions/2389183/flash-closest-point-to-movieclip/2407510#2407510

관련 문제