flash as3 - 获取父剪辑相对于孩子位置的位置

时间:2011-12-07 17:18:29

标签: flash actionscript-3

我正在尝试使用flash as3中的输入框架事件在父项和子剪辑之间绘制一条线,因为它们被拖动或在屏幕上移动。我实际上使用以下代码工作:

/*private function drawLine(childCircle,parentCircle):void
{
    parentCircle.graphics.clear();
    parentCircle.graphics.lineStyle(lineWeight,lineColor);
    parentCircle.graphics.moveTo(0,0);
    parentCircle.graphics.lineTo(childCircle.x,childCircle.y);
}*/

但问题是可以从父级创建多个子圈,因此上面的函数清除了从父级到第一个子级的行,支持从父级到第二个子级的绘制。所以我想我需要将这条线添加到子圆而不是父线,但是当我这样做时,我无法获得相对于孩子的父圆的正确坐标。这是我的代码:

private function drawLine(childCircle,parentCircle):void
{
    //trace (parentCircle.x + "|" + childCircle.x);
    childCircle.graphics.clear();
    childCircle.graphics.lineStyle(lineWeight,lineColor);
    childCircle.graphics.moveTo(0,0);
    childCircle.graphics.lineTo(parentCircle.x,parentCircle.y);
}

任何人都知道我会这样做吗?我看到了globalToLocal和localToGlobal的函数,但我不想要全局位置 - 只是位置一级。我试过这个:

private function drawLine(childCircle,parentCircle):void
{
    trace (parentCircle.x + "|" + childCircle.x);
    childCircle.graphics.clear();
    childCircle.graphics.lineStyle(lineWeight,lineColor);
    childCircle.graphics.moveTo(0,0);
    childCircle.graphics.lineTo(-childCircle.x,-childCircle.y);
}

并且它最终出现在正确的位置,但它导致了我正在使用的drag-ease方程式的问题,因为该行被绑定到子剪辑而不是父剪辑。

3 个答案:

答案 0 :(得分:1)

我会考虑创建一个负责自己的线条绘制的Child类,而不是从父类绘制。我也会忘记Enter Frame事件,而只是在Child移动时实现线条图...在这种情况下,它由MouseMove事件触发,但它可以是任何其他类型的触发器。

private var container:DisplayObject;
private var line:Shape = new Shape();

public function Child()
{
      //add the Line to be drawn as a Shape
      addChild( line );

      //everything starts after the Child has been added to the Stage
      addEventListener( Event.ADDED_TO_STAGE , onAdded );
}

private function onAdded( event:Event):void
{
      // remove this listener
      removeEventListener( Event.ADDED_TO_STAGE , onAdded );

      //identify the container
      container = this.parent;

      //listen to a Mouse Down event
      addEventListener( MouseEvent.MOUSE_DOWN , onMouseDown );

      //the Stage listens to a Mouse Up event
      stage.addEventListener( MouseEvent.MOUSE_UP , onMouseUp );
}

private function onMouseDown( event:MouseEvent):void
{
     //start listening to the Mouse Move when the mouse is down
      addEventListener( MouseEvent.MOUSE_MOVE , onMouseMove );
}

private function onMouseMove( event:MouseEvent):void
{ 
     //draw the line from the parent coordinates to the child coordinates
     line.graphics.clear();
     line..graphics.lineStyle(1);
     line.graphics.moveTo( container.x , container.y );
     line.graphics.lineTo( this.x , this.y );
}

private function onMouseUp( event:MouseEvent):void
{
      removeEventListener( MouseEvent.MOUSE_MOVE , onMouseMove );
}

答案 1 :(得分:0)

你可以这样做:

private function drawLines():void
{
    // Clear graphics, prep line style
    parentCircle.graphics.clear();
    parentCircle.graphics.lineStyle(lineWeight,lineColor);

    // Create an array of all children to draw to, then 
    // loop through that array drawing a line to each 
    var ar:Array = [child1, child2, child3];
    var len:uint = ar.length;
    for ( var i:uint=0; i<len; i++ )
    {
        drawLine( ar[i], parentCircle );
    }
}

private function drawLine(childCircle,parentCircle):void
{
    parentCircle.graphics.moveTo(0,0);
    parentCircle.graphics.lineTo(childCircle.x,childCircle.y);
}

答案 2 :(得分:0)

这将为您提供一个可拖动的父剪辑,该剪辑还包含通过行连接到父级的可拖动子剪辑。

圈子类:

package
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;

    public class Circle extends Sprite
    {
        private var _fill:int;

        public function Circle(fill:int=0xFFFFFF)
        {
            super();

            this._fill = fill;

            this.addEventListener(MouseEvent.MOUSE_DOWN, _startDrag);
            this.addEventListener(MouseEvent.MOUSE_UP, _stopDrag);

            draw();
        }

        public function draw():void
        {
            this.graphics.lineStyle(2);
            this.graphics.beginFill(this._fill);
            this.graphics.drawCircle(0, 0, 7);
            this.graphics.endFill();
        }

        private function _startDrag(e:MouseEvent):void
        {
            e.stopPropagation()
            this.startDrag();
        }

        private function _stopDrag(e:MouseEvent):void
        {
            e.stopPropagation()
            this.stopDrag();
        }
    }
}

主要课程(舞台课程)

package
{
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.events.Event;

    [SWF(backgroundColor="#EDEDED", frameRate="30", width="500", height="500")]
    public class Main extends Sprite
    {
        private var _parentCircle:Circle;
        private var _children:Array = [];

        public function Main()
        {
            _parentCircle = new Circle(0xFF0000);
            this.addChild(_parentCircle);
            _parentCircle.x = 250;
            _parentCircle.y = 250;

                    // create some children
            for (var i:int=0; i < 5; i++)
            {
                var childCircle:Circle = new Circle();
                childCircle.x = Math.random() * 500 - 250;
                childCircle.y = Math.random() * 500 - 250;
                _parentCircle.addChild(childCircle);
            }

            this.addEventListener(Event.ENTER_FRAME, _redraw);
        }

        private function _redraw(e:Event):void
        {
            _parentCircle.graphics.clear();
            _parentCircle.graphics.lineStyle(1);

            for (var i:int=0; i < _parentCircle.numChildren; i++)
            {
                var child:DisplayObject = _parentCircle.getChildAt(i);
                _parentCircle.graphics.moveTo(0, 0);
                _parentCircle.graphics.lineTo(child.x, child.y);
            }

            _parentCircle.draw();
        }
    }
}