2011-12-07 14 views
0

flash as3でenterフレームイベントを使用して、親クリップと子クリップがドラッグされたり画面上を移動したりするときに線を描画しようとしています。私は実際にこれは次のコードを使用して作業していた: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); 
}*/ 

をしかし、問題は、複数の子円は親から作成することができ、そしてその機能は上記の賛成で最初の子に親から行をクリアしています親から2番目の子に描画します。だから私は、親の代わりに子円に線を追加する必要があると考えましたが、私はこれを行うと子の親円の正しい座標を得ることができません。これは私のコードです:

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(); 
     } 
    } 
} 
関連する問題