Dynamically centering a MovieClip on load

Hey guys,

This is my first question to Kirupa.com :slight_smile:

Here’s my function:

[AS]
leftBnd = 100;
rightBnd = 730;
topBnd = 15;
lowBnd = 360;

insertMovie = function(swf){
with(_root){
emptyClip.unloadMovie();
emptyClip._visible = false;
emptyClip.loadMovie(swf);

	trace("width: " + emptyClip._width);
	trace("height: " + emptyClip._height);
	
	xpos = (rightBnd-leftBnd)-emptyClip._width;
	xpos = (xpos/2) + leftBnd;
	trace("x: " + xpos);

	ypos = (lowBnd-topBnd)-emptyClip._height;
	ypos = (ypos/2) + topBnd;
	trace("y: " + xpos);

	emptyClip._x = xpos;
	emptyClip._y = ypos;

	emptyClip._visible = true;
}

}
[/AS]

Now, the first time this function is executed I get 0 and 0 for the emptyClip’s width and height even though it has run loadMovie(). Why is this? I would think that it would return the width and height of the newly loaded movie… because of this erroneous width and height the movie doesn’t center correctly unless the function is run twice to load the same movie.

The second time it is run it returns the w and h of the first loaded movie…

the third time it returns the w and h of the second movie, etc, etc.

Any help?

thanks

georges

make it’s reg point in the middle and set it’s _x to Stage.width/2
and _y to Stage.height

see this:

http://www.kirupaforum.com/forums/showthread.php?threadid=26238&highlight=stage :slight_smile:

Alright, those are some pretty interesting answers and they helped me, but I still have a problem.

The SWFs that I’m loading in seem to have different relative points on which they center. How does one set the registration point on a MovieClip?

Some SWFs seem to center relative to upper-left corner, some relative to upper-center point. One even centers relative to lower-center! :slight_smile:

Any tips?

align window…