views:

264

answers:

3

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!

A: 

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

ssergei
A: 

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;
}
majman
But you don't know where the registration point is on the loaded MC. That would work only if the registration point was in the corner everytime.
ssergei
Good point. Is there a work around?
motionman95
Ah, right. So maybe create an empty MC and load the movie in there. Then that will have the top left corner as its registration point. var container = createEmptyMovieClip("container", getNextHighestDepth()); container.loadMovie("movie");// on finished loadingcontainer._x = container._width/2 * -1; etc...
majman
+1  A: 

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.

F. Espinoza
Never mind. First post fail, this is for AS3.
F. Espinoza