0

我想在舞台调整大小时将一个大电影剪辑(1400 像素宽)居中。这个大电影剪辑在某些事件上向右移动,所以我不能使用这样的代码:

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

有没有办法使用它从 0 开始的偏移量(舞台“视口”的左侧)并使用该偏移量进行居中?

影片剪辑仅在 x 中发生变化。

4

2 回答 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.

于 2010-11-19T04:06:13.747 回答
0

我更喜欢为所有内容制作容器精灵。在容器内部,所有内容都按照场景始终为 400x300(或任何其他固定大小,无论您需要什么纵横比)进行测量。调整场景大小时,我只调整容器的大小和居中以适合内部:

//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;

这样,您可以处理容器中任意数量的剪辑,而不仅仅是一个。容器不使用所有屏幕空间,但保留纵横比。如果你想使用所有的屏幕空间,你必须考虑你的舞台的动态布局——只有你才能正确地做到这一点。

于 2010-11-19T09:09:57.583 回答