【问题标题】:Actionscript 3 Centering a Moving Object on stage resizeActionscript 3在舞台调整大小时将移动对象居中
【发布时间】:2010-11-19 17:09:57
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: flash actionscript-3 centering stage


    【解决方案1】:

    当一个对象被调整大小时,我们可以说它的比例发生了变化。天平很好,因为它们允许我们以百分比工作。给定任何百分比变化,我们可以将相同的变化应用于任何其他对象以获得相对位置。看这里:

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

    希望对你有用。

    【讨论】:

    • 你认为什么样的错误?请为提问者解释。
    【解决方案2】:

    我更喜欢为所有内容制作容器精灵。在容器内部,一切都按照场景始终为 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;
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多