Center movieclip on point

2019-07-25 08:06发布

问题:

I'm dynamically loading movie clips (SWFS) into attached clips (from the library). The loaded movies don't all have center points, meaning that their registration point isn't directly in its center. This poses a problem when I load them into the attached clips, because they don't center on the attached clips, which is what I want them to do. Is there a way to center a movieclip based on width and height instead of registration point?

Thanks in advance!

回答1:

Use getBounds.

var bounds:Rectangle = loadedClip.getBounds(loadedClip);

bounds.x and bounds.y will be {0,0} for a top left aligned movie clip. Any other value tells you how much off center it is.

If your loaded clip is loadedClip and its parent is containerClip, the following will work.

loadedClip.x = (container.width - loadedClip.width)/2 - loadedClip.getBounds(loadedClip).x;
loadedClip.y = (container.height - loadedClip.height)/2 - loadedClip.getBounds(loadedClip).y;

If the clips involved in this have been scaled, then you have to adjust for the scaling as follows:

loadedClip.x = (container.width - loadedClip.width) / 2 - (loadedClip.getBounds(loadedClip).x * loadedClip.scaleX);
loadedClip.y = (container.height - loadedClip.height) / 2 - (loadedClip.getBounds(loadedClip).y * loadedClip.scaleY);

I hope this helps.



回答2:

You could just change the registration point of the MC to be centered using this class: Dynamic MovieClip Registration with AS2



回答3:

When the load completes, you can get the width and height of the loaded movies, and adjust the x & y position accordingly:

function onLoadComplete(){
 loadedmc._x = loadedmc._width/2 * -1;
 loadedmc._y = loadedmc._height/2 * -1;
}