确切的元素位置actionscript3

时间:2017-07-18 12:03:53

标签: actionscript-3 flex actionscript

我在尝试在AS3上划一条线时遇到了一些麻烦。

绘图是一个简单的部分,但棘手的部分是如何获得组件的位置。

我试图设置一个等级,是儿子们通过线路与父亲联系在一起。 我有屏幕上的结构和组件,但当我尝试在节点之间画一条线时,我无法找到儿子的位置。

    public function drawLines():void{
                for(var i:int=1; i<= _maxLevel ; i++){
                        var vGroup:*=treeLevel.getElementAt(i);
                        for(var j:int = 1; j<vGroup.numChildren ;j++){
                            var element:* = vGroup.getElementAt(j);
                            trace(element.fatherJoin);//a checkbox for the union
                            trace(element.sonJoin);//another checkbox for the union
                            var coord:* = buscarCoord(element.father,i-1);//with this function I get the father checkbox
                            coord.graphics.lineStyle(3, 0xFF0000, 1 );

//onwards is the fail code, I can't get the correct x and y to draw.
                            var pt:Point = new Point(element.fatherJoin.x,element.fatherJoin.y);
                            pt = this.localToGlobal(pt);
                            coord.graphics.lineTo(pt.x,pt.y);
                        }
                }
            }

通过addElement在vgroup上设置元素,并且我看起来x = 0和y = 0。

任何人都知道如何获得正确的坐标。这个元素?

感谢。

1 个答案:

答案 0 :(得分:1)

您可能需要的是:

// Create an empty point of (0,0).
var aPoint:Point = new Point;

// Get the global coordinates of the object you want.
aPoint = element.fatherJoin.localToGlobal(aPoint);

// Translate it to the coordinates of your canvas.
aPoint = coord.globalToLocal(aPoint);

// Now draw.
coord.graphics.lineTo(aPoint.x, aPoint.y);

请注意, element.fatherJoin coord 必须(不一定是直接的,他们可能是孩子的孩子)附加到舞台上,否则 localToGlobal globalToLocal 不会产生正确的结果。

UPD:我试过了。

var C:Sprite = new Sprite;
var Z:Sprite = new Sprite;

Z.x = 100;
Z.y = 200;

C.x = 300;
C.y = 400;

// Z is not attached to anything.
trace(Z.globalToLocal(new Point));
// output: (x=-100, y=-200)

C.addChild(Z);

// C is not attached to stage.
trace(Z.globalToLocal(new Point));
// output: (x=-400, y=-600)

addChild(C);

// C is attached to stage.
trace(Z.globalToLocal(new Point));
// output: (x=-400, y=-600)
相关问题