2011-05-24 17 views
1

我试图缩放一个容器/父级电影剪辑,以便我有效地放大到其子节点引用的点。我已经想出了如何使用globalToLocal在舞台中心获得该点,但问题是容器剪辑的注册点是(并且需要保留)在左上角,所以当我缩放容器剪辑时该点不会留在屏幕的中心。这里是我的代码:闪光as3放大(放大)到一个剪辑内的特定点

//修改为:

var stageCenter = new Point(int(stage.stageWidth/2),int(stage.stageHeight)/2); 
    var parPointLocal = parRef.globalToLocal(stageCenter); 
    TweenMax.to(treeClip,.5,{x:parPointLocal.x,y:parPointLocal.y,onComplete:doZoom}); 

    function doZoom():void { 
     var zoomPoint = zoomToMember(treeClip,stageCenter,2); 

     function zoomToMember(target:MovieClip, center:Point, scale:Number):Point { 
      var m:Matrix = new Matrix(); 
      m.translate(-center.x, -center.y);//move the center into (0,0) 
      m.scale(scale, scale);//scale relatively to (0,0) (which is where our center is now) 
      m.translate(center.x, center.y);//move the center back to its original position 
      return m.transformPoint(new Point());//transform (0,0) using the whole transformation matrix to calculate the destination of the upper left corner 
     } 

     TweenMax.to (treeClip,.5,{x:zoomPoint.x,y:zoomPoint.y,scaleX:2,scaleY:2}) 

    } 

当我这样做,缩放点最终被周围的地方“梅布尔Greer的玩具商店” - 这是我想象是舞台的中央点之前树剪辑被补间,以便“乔恩安德森”将在舞台的中心。

screenshot

回答

3

您需要缩放和吐温x和y属性之后,计算左上角的目标位置。

最简单的方法是使用矩阵:

function scale(target:MovieClip, center:Point, scale:Number):Point { 
    var m:Matrix = new Matrix(); 
    m.translate(-center.x, -center.y);//move the center into (0,0) 
    m.scale(scale, scale);//scale relatively to (0,0) (which is where our center is now) 
    m.translate(center.x, center.y);//move the center back to its original position 
    return m.transformPoint(new Point());//transform (0,0) using the whole transformation matrix to calculate the destination of the upper left corner 
} 
+0

谢谢 - 我有点明白(对不起),但是当我实现它,它并不完全似乎被放大到正确的位置。我上面编辑了我的描述以显示修改后的代码。你可以提供的任何帮助将是伟大的 – mheavers 2011-05-24 17:30:03

+0

哦 - 另外 - 如果movieclip(目标)没有被使用,为什么你包含影片剪辑作为函数的参数?它应该是? – mheavers 2011-05-24 18:09:01

+0

是的,包括目标实际上并没有使用:)。但是,如果中心点实际上是在全局坐标空间中表示的,那么您需要执行'center = target.globalToLocal(center);',这会使目标再次出现。 – back2dos 2011-05-25 10:54:24