Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
flash as3-获取父剪辑';s相对于子对象的位置';s位置_Flash_Actionscript 3 - Fatal编程技术网

flash as3-获取父剪辑';s相对于子对象的位置';s位置

flash as3-获取父剪辑';s相对于子对象的位置';s位置,flash,actionscript-3,Flash,Actionscript 3,我试图在flash as3中使用enter frame事件在父剪辑和子剪辑在屏幕上拖动或移动时在它们之间画一条线。实际上,我使用以下代码实现了这一点: /*private function drawLine(childCircle,parentCircle):void { parentCircle.graphics.clear(); parentCircle.graphics.lineStyle(lineWeight,lineColor); parentCircle.gr

我试图在flash as3中使用enter frame事件在父剪辑和子剪辑在屏幕上拖动或移动时在它们之间画一条线。实际上,我使用以下代码实现了这一点:

/*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);
}

它最终位于正确的位置,但这会导致我使用的阻力缓和方程出现问题,因为该线系在子剪辑而不是父剪辑上。

您可以执行以下操作:

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);
}
专用函数drawLines():void
{
//清晰的图形,线条风格
parentCircle.graphics.clear();
parentCircle.graphics.lineStyle(线宽、线颜色);
//创建要绘制的所有子对象的数组,然后
//在数组中循环,在每个数组中画一条线
var-ar:Array=[child1,child2,child3];
var len:uint=ar.length;

对于(var i:uint=0;i这将为您提供一个可拖动的父剪辑,其中还包含通过线连接到父剪辑的可拖动子剪辑

圆圈类:

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();
        }
    }
}
主班(舞台班)

包
{
导入flash.display.DisplayObject;
导入flash.display.Sprite;
导入flash.events.Event;
[SWF(backgroundColor=“#eded”,frameRate=“30”,width=“500”,height=“500”)]
公共类Main扩展了Sprite
{
私有变量parentCircle:Circle;
私有变量_子项:数组=[];
公共功能Main()
{
_parentCircle=新的圆(0xFF0000);
this.addChild(_parentCircle);
_parentCircle.x=250;
_parentCircle.y=250;
//创造一些孩子
对于(变量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\u FRAME,\u redraw);
}
私有函数重新绘制(e:事件):无效
{
_parentCircle.graphics.clear();
_parentCircle.graphics.lineStyle(1);
for(变量i:int=0;i<\u parentCircle.numChildren;i++)
{
变量child:DisplayObject=\u parentCircle.getChildAt(i);
_parentCircle.graphics.moveTo(0,0);
_parentCircle.graphics.lineTo(child.x,child.y);
}
_parentCircle.draw();
}
}
}

>P>而不是从父节点中提取,我会考虑创建一个子类来照顾它自己的线图。我也会忘记输入帧事件,而只在孩子移动时实现行绘制……在这种情况下,它是由MUMeMoFe事件触发的,但它可以是任何其他类型的触发器。
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 );
}
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 );
}