views:

199

answers:

2

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?

Edit: this is somewhat what I need:

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;

      }
}

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.

An old AS2 script that do that, but I'm not sure how to convert it:

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;
    }
}
A: 

if you use either hittest or rollover rollout events then you can use mousex mousey to find the mouse position. If you need closest point to mouse then you are in a wonderful world of trial and error. See;

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

shortstick
A: 

The code you pasted would look something like this in 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;
 }
}

It's kind of cool and though it's not perfect, works reasonably fast. So, I'd check it with your actual shape. It might be good enough.

Juan Pablo Califano
It does work, but I changed container.x + (container.width / 2) for (container.x + container.width) / 2. Do you know if there's a way to have two of these objects? I have tried by making a copy of the MC and a copy of the script with different variables but by some reason, the one I put on the right messes up.
peroyomas
I changed it again to container.x + container.width / 4 and works well in all my cases.
peroyomas