2012-06-28 4 views
2

한 계층에서 다른 계층으로 다시 양육 할 때 표시 객체의 동일한 변형을 유지하는 데 문제가 있습니다. 아래 이미지에서 중첩 된 자식 "mc2"가있는 무비 클립 "mc1"을 볼 수 있습니다. "mc2"에는 자체 자식 "mc3"이 있습니다. 각 자손은 어떻게 든 변형됩니다 (회전, 축척 등). 계층 구조 "mc1"에서 "mc4"를 가져 와서 계층 구조 "do1"의 "do3"에 넣기를 원합니다 (또한 각 수준마다 다른 변환이 있음).표시 객체를 다시 부모로 지정할 때 변형 행렬 조작

그렇다면 사각형 ("mc4")을 다른 위치에 배치하지 않고 어떻게 배치합니까? (끌어서 놓기를 상상해보십시오).

Transform.concatenedMatrix 속성을 사용하여 시도했지만 손실되었습니다.

감사합니다. 당신이 시도 할 수

nested movie clips

답변

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; 

}  
1

, 그것은 (당신이 할 모든 규모/X/Y/회전 속성을 수정할 때) 내 상황에서 작동하지만, 변환 행렬을 사용하는 경우 제대로 작동하지 않을 수 있습니다. 몇 가지 실험 후

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; 

} 
+0

감사합니다! 이것은 나를 위해 일했지만 이것은 내가 얻고 싶었던 대답이 아닙니다. –

+0

답변 해 주셔서 다시 한 번 감사드립니다. 나는 내 대답을 올렸다. 아마 그것은 당신에게 유용 할 것입니다. –

관련 문제