2010-06-03 26 views
1

如何在ActionScript中的Actionscript 3中製作跟隨光標的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

您粘貼會是這個樣子在AS 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)for(container.x + container.width)/ 2.你知道是否有辦法讓這些對象中的兩個對象?我曾經嘗試製作一個MC副本和一個帶有不同變量的腳本副本,但由於某種原因,我放在右邊的那個混亂起來。 – peroyomas 2010-06-04 05:08:22

+0

我再次將其更改爲container.x + container.width/4,並且在我的所有情況下運行良好。 – peroyomas 2010-06-06 15:12:20

0

如果您使用則hitTest或側翻部署事件,那麼你可以使用mousex像老鼠找到鼠標位置。如果你需要最接近鼠標的地方,那麼你正處在一個美妙的嘗試和錯誤的世界。看到;

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