0

I want to center a big movieclip (1400 px wide) on my stage on stage resize. This big movieclip moves to the right on certain events so I can't use code like this:

currentPage.x = ((stage.stageWidth/2) - (currentPage.width/2))

Is there a way to maybe use its offset from 0 (the left side of the stage 'viewport') and use that offset in centering?

The movieclip only changes in x.

2 Answers 2

0

When a object is resized, we can say its scale has changed. Scales are nice because they allow us to work in percentages. Given any percentage change, we can apply the same change to any other object to get a relative position. Look here:

var previousStageWidth:Number;

public function handleResize():void {
    //calculate the difference in stage with as a percentage
    var widthScale:Number = previousStageWidth / stage.stageWidth;

    //scale the x value by the same amount
    myDisplayObject.x *= widthScale;

    //update the previous value for the next resize
    previousStageWidth = stage.stageWidth;
}

Hopefully that works out for you.

1
  • What kind of errors do you figure? Please explain for the question asker. Nov 19, 2010 at 15:31
0

I'm prefer to make container sprite for all contents. Inside of container, everything is measured as if scene was always 400x300 (or any other fixed size, whatever aspect ratio you need). When scene is resized, I'm resizing and centering only the container to fit inside:

//not really tested because my real code is more complex, so watch out...
var bounds:Rectangle = container.getRect(null);
//scale factor to fit inside
var scaleFactor:Number = Math.min(stage.stageWidth / bounds.width, stage.stageHeight / bound.height);
container.scaleX = container.scaleY = scaleFactor; //scaling
//centering
container.x = (stage.stageWidth - container.width) * 0.5;
container.y = (stage.stageHeight - container.height) * 0.5;

This way you can handle any number of clips in container, not just one. Container not uses all screen space, but preserves aspect ratio. If you want to use all screen space, you have to think out dynamic layout of your stage - something only you can do properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.