2

从一个层次结构重新设置为另一个层次结构时,我在保持显示对象的相同变换时遇到问题。在下图中,您可以看到包含嵌套子“mc2”的影片剪辑“mc1”。“mc2”有自己的孩子“mc3”等等。每个后代都以某种方式进行了转换(旋转、缩放等)。我想从层次结构“mc1”中取出“mc4”并放入层次结构“do1”的“do3”(每个级别也有不同的转换)。

那么如何在不改变其外观(位置除外)的情况下将方形“mc4”放入其他层次结构中?(想象一下拖放)。

我尝试使用 Transform.concatenedMatrix 属性来做,但我迷路了。

谢谢!

嵌套影片剪辑

4

2 回答 2

3

经过几次实验,我自己找到了正确的答案:

    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.geom.Matrix; 


function changeParent ( displayObject : DisplayObject, newParent : DisplayObjectContainer, depth : int = -1 ) : void {

    var concatenedChildMatrix       : Matrix = displayObject.transform.concatenatedMatrix;

    var concatenedNewParentMatrix   : Matrix = newParent.transform.concatenatedMatrix;

    // invert matrix. It couses visual removal of transformations (movie clip looks like it wasn't transformed )
    concatenedNewParentMatrix.invert();

    concatenedChildMatrix.concat( concatenedNewParentMatrix );

    // if you want to add clip not on top level
    if ( depth >= 0 ) {
        newParent.addChildAt( displayObject, depth );
    } else {
        newParent.addChild( displayObject );
    }

    // assign matrix back
    displayObject.transform.matrix = concatenedChildMatrix;

}       
于 2012-07-04T10:29:16.560 回答
1

你可以试试这个,它适用于我的情况(当你所做的只是修改 scale/x/y/rotation 属性时),但在使用转换矩阵时可能无法正常工作。

function changeParent(displayObj:DisplayObject, newParent:DisplayObjectContainer, depth:Number = -1, retainRelativeSize:Boolean = false):void {
var tmpParent:DisplayObjectContainer = displayObj.parent;

    while (tmpParent) {
        displayObj.scaleX *= tmpParent.scaleX;
        displayObj.scaleY *= tmpParent.scaleY;
        displayObj.rotation += tmpParent.rotation;
        tmpParent = tmpParent.parent;
    }

    tmpParent = newParent;
    while(tmpParent){
        displayObj.scaleX = displayObj.scaleX / tmpParent.scaleX;
        displayObj.scaleY = displayObj.scaleX / tmpParent.scaleY;
        displayObj.rotation -= tmpParent.rotation;
        tmpParent = tmpParent.parent;
    }

    var point1:Point= displayObj.localToGlobal(new Point());
    var point2:Point = newParent.globalToLocal(point1);

    if (depth >= 0) {
        newParent.addChildAt(displayObj, depth);
    }else {
        newParent.addChild(displayObj);
    }

    displayObj.x = point2.x;
    displayObj.y = point2.y;

}
于 2012-06-28T22:51:36.910 回答