2011-12-07 27 views
0

我试图在flash as3中使用一个输入框架事件来在父屏幕和子剪辑之间绘制一条线,因为它们在屏幕上被拖动或移动。其实,我有这个工作使用下面的代码: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); 
} 

,并在正确的位置结束了,但它会导致一个问题,我使用的,因为该行是拴在孩子夹,而不是家长的阻力,便于方程。

回答

1

而是从父绘制的,我会考虑创建一个子类,它自己的画线的照顾。我也会忘记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); 
} 
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); 
} 
0

这会给你一个拖动父剪辑,也包含通过线路连接到父拖动子剪辑。

Circle类:

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(); 
     } 
    } 
}