Flash: Closest point to MovieClip
- by LiraNuna
I need to constrain a point inside a DisplayObject given to me by the artist.
I got it working but only for the occations where the cursor is still inside bounds.
The limited object is called limited.
function onSqMouseMove(event:MouseEvent) {
if(bounds.hitTestPoint(event.stageX, event.stageY, true)) {
limited.x = event.stageX;
limited.y = event.stageY;
} else {
/* Find closest point in the Sprite */
}
}
limited.addEventListener(MouseEvent.MOUSE_DOWN, function(event:MouseEvent) {
stage.addEventListener(MouseEvent.MOUSE_MOVE, onSqMouseMove);
});
limited.addEventListener(MouseEvent.MOUSE_UP, function(event:MouseEvent) {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onSqMouseMove);
});
How do I go about implementing the other half of the function? I am aware Sprite's startDrag accepts arguments, where the second one is the constraint rectangle, but in my case, bounds are an arbitrary shape.
When the object is dragged outside the bounds, I want to calculate the closest point from the cursor to bounds' polygon.
Just to note that bounds can have 'holes'.