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