PDA

View Full Version : centering loaded content



kritikal
October 28th, 2005, 05:57 PM
ok here's my problem,

I have an index.swf that it's dimensions are 1020x610, now when I load content in a container in the index.swf it will be centered using an onResize stage listener, that works fine but only if the loaded content is 1020x610, I've also used an onEnterFrame that checks if the width and height are !=0 so that way it waits for it to load before centering the content.

What should I do to always keep my content centered. Lets say I want to load a swf that is smaller (ie the 1st frame only contains 1 element that is smaller than the stage dimensions) I'm not sure if I'm explaining this well....

clica2x
October 29th, 2005, 06:27 PM
TRY this:

_root.your_mc._x = Math.round(Stage.width / 2);
_root.your_mc._y = Math.round(Stage.height / 2);

it works for me...



Marcio Henrique - Portugal
clica2x@hotmail.com

Jeff Wheeler
October 29th, 2005, 06:50 PM
That won't make it exactly centered. Instead, use this:


_root.your_mc._x = Math.round((Stage.width/2)-(_root.your_mc._width/2));
_root.your_mc._y = Math.round((Stage.height/2)-(_root.your_mc._height/2));

Barn
October 29th, 2005, 07:44 PM
Never mind. Just realized this was about dynamically loaded content.

kritikal
October 29th, 2005, 08:42 PM
I actually use


var stageXCenter:Number = Stage.width * .5;
var stageYCenter:Number = Stage.height * .5;

container._x = stageXCenter - container._width/2 ;
container._y = stageYCenter - container._height/2;


but what I meant was when the contents of the container are from another swf it's a different story especially when the first loaded frame has an object smaller than the last frame and if the dimensions of the whole thing is smaller than the main swf

Barn
October 29th, 2005, 09:11 PM
And then, still another, you can perform the subtraction first, and THEN divide by two:


container._x = (Stage.width-container._width)/2;
container._y = (Stage.height-container._height)/2;

Ain't math wonderful?

Jeff Wheeler
October 29th, 2005, 09:49 PM
That works too :)