/ Published in: ActionScript
Useful for draggable clips that should be constrained to an area.
Expand |
Embed | Plain Text
/* * Checks a clip's position and size and repositions inside an area * *@param clip Movieclip to reposition *@param area An object indication the area that the clip should be constrained to *@param reposition Boolean indicating if movieclip's position should be modified *@param hSpacing Horizontal margin from the border *@param vSpacing Vertical margin from the border *@returns An object with the x and y values that are inside the area */ function constrainToArea(clip:MovieClip, area:Object, reposition:Boolean, hSpacing:Number, vSpacing:Number):Array { if (!hSpacing) { var hSpacing:Number = 0; } if (!vSpacing) { var vSpacing:Number = 0; } var clipPoint:Object = new Object(); clipPoint.x = clip._x; clipPoint.y = clip._y; clip._parent.localToGlobal(clipPoint); // Return same position var newX = clip._x; var newY = clip._y; // Reposition on x var overX = area.width-(clipPoint.x+clip._width); if (overX<0) { newX -= Math.abs(overX)+hSpacing; } if (clipPoint.x<0) { newX += Math.abs(clipPoint.x)+hSpacing; } // Reposition on y var overY = area.height-(clipPoint.y+clip._height); if (overY<0) { newY -= Math.abs(overY)+vSpacing; } if (clipPoint.y<0) { newY += Math.abs(clipPoint.y)+vSpacing; } if (reposition) { clip._x = newX; clip._y = newY; } return {x:newX, y:newY}; }
You need to login to post a comment.
