title: AS3 Calling a child's index during a for each
body: <p>I have a function that uses a mouse click event to create a child of a symbol at the mouse's coordinates. The instance is then pushed to an array.</p>
  
  <p>I am now trying to make a new function that allows you to drag/drop a child when the mouse is within 10 pixels of it. When the child is dropped, I want it to rotate to face the next child in the array. I am currently communicating with each child in the array using a <code>for each</code> loop.</p>
  
  <p><strong>The bit I'm having trouble with</strong> is getting the index value of the child being dropped and setting it to a variable.</p>
  
  <p>For reference, <code>ins_trailPoint</code> is the variable that creates the child and <code>tPoint</code> is the name of the array containing the children. <code>child</code> is the variable trying to acquire the index value of the dropped child.</p>
  
  <pre><code>function movePoint(Event:MouseEvent):void {
      for each (var ins_trailPoint in tPoint) {
          var child:int = event.target.parent.getChildIndex(event.target);
          var px:Number = mouseX - ins_trailPoint.x;
          var py:Number = mouseY - ins_trailPoint.y;
          var dist = Math.sqrt(px*px + py*py);
  
          if (dist&lt;10) {
              stage.removeEventListener(MouseEvent.CLICK, addPoint);
              stage.addEventListener(MouseEvent.MOUSE_UP, dropPoint);
              ins_trailPoint.startDrag();
  
              function dropPoint(event:MouseEvent):void {
                  ins_trailPoint.stopDrag();
                  var dx:Number = tPoint[child+1].x - ins_trailPoint.x;
                  var dy:Number = tPoint[child+1].y - ins_trailPoint.y;
                  var radians:Number = Math.atan2(dy,dx);
                  ins_trailPoint.rotation = radians * 180/Math.PI;
  </code></pre>