AS3在面具内放置可拖动容器的中心

时间:2010-08-02 11:25:04

标签: flex flash actionscript-3 actionscript

HI,我有容器动画片段和一个具有当前屏幕高度和宽度的遮罩层。连接器总是大于掩模夹。所以我需要在面具剪辑的中心缩放容器。 something link in MicroSoft XL zoom controller at the bottom right

是否曾经有等式或任何其他演示链接? 在此先感谢!!!

编辑1

private function slider (event:Event) {
    //event.target.value = 0 to 1  
    // possible values are 0, 0.1, 0.2, 0.3, ... 0.9, 1
    // mcMask.x =  mcMask.x = 0;
    // mcMask.width = stage.StageWidth, mcMask.height = stage.StageHeight
    // mcContainer.x and mcContainer.y may vary .. its greater than the mcMask clip. 
    // So its need to be drag on the mask clip. I have placed a lots of images in the container something like map. 
    // If the slider changes the values then the map(mcContainer) need to zoom to scale came from 'event.target.value'.

    // i want to zoom the mcContainer inside the mask, not the mouse cliked point, I want the current center 
    // posistion of the container with resepect to the  mask clip.

    mcContainer.scaleX = mcContainer.scaleY = (event.target.value);
    mcContainer.x = (mcMask.width - mcContainer.width)/2;
    mcContainer.y = (mcMask.height - mcContainer.height)/2;

    // I tried this but if i drag this mcContainer to the left or right it should not locate the center point.
}

任何希望??? :(

编辑2

源代码:http://www.4shared.com/file/08X5mG99/AS3_Zooming.html

2 个答案:

答案 0 :(得分:1)

好吧,当你围绕一个点缩放时,你必须知道该点相对的位置,然后以与你正在调整大小的剪辑相同的比例缩放它。因此这样的事情应该有用(注意,这没有经过测试,但尝试稍微调整一下):

- 编辑 -

private function zoomContainer (event:Event):void {
        var relativeZoom:Number = event.target.value / mcContainer.scaleX;

        var origin:Point = new Point(stage.stageWidth/ 2, stage.stageHeight / 2);
        var centerPoint:Point = mcContainer.globalToLocal(origin);
        centerPoint.x *= relativeZoom;
        centerPoint.y *= relativeZoom;
        var newCenterPoint:Point = mcContainer.localToGlobal(centerPoint);
        var offset:Point = origin.subtract(newCenterPoint);

        mcContainer.x += offset.x;
        mcContainer.y += offset.y;
        mcContainer.scaleX *= relativeZoom; 
        mcContainer.scaleY *= relativeZoom;
    }

答案 1 :(得分:0)

假设两个剪辑都在左上方有注册点,那么您可以使用

mcContainer.x = (mcMask.width - mcContainer.width)/2;
mcContainer.y = (mcMask.height - mcContainer.height)/2;

这将使容器夹子居中 如果你想缩放容器剪辑,你需要这个

mcContainer.scaleX = mcContainer.scaleY = Math.min(mcMask.width/mcContainer.width, mcMask.height/mcContainer.height);

Math.min更改为Math.max会将“缩放”样式从显示全部更改为填充遮罩区域。如果您要进行缩放和定位,则需要在定位前进行缩放。