title: FLASH/AS3: Figure that follows the cursor yet constrained to an irregular area
body: <p>How can I make in Actionscript 3 in Flash a MovieClip that follow the cursor, but is constrained to the irregular shape of another MovieClip?</p>
  
  <p>Edit: this is somewhat what I need:</p>
  
  <pre><code>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;
  
        }
  }
  </code></pre>
  
  <p>What I want to accomplish is to make that the cursor MC still "follow" the cursor when is outside the container MC, but can't escape from it.</p>
  
  <p>An old AS2 script that do that, but I'm not sure how to convert it:</p>
  
  <pre><code>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 &gt; .5) is more accurate, (dX &gt; 10) is less.
          } while ((Math.abs(dX)&gt;accuracy) || (Math.abs(dY)&gt;accuracy));
          _x = tX;
          // we're done, set the final position.
          _y = tY;
      }
  }
  </code></pre>