# swapChildren - referencing the items..?

**URL:** <https://forum.kirupa.com/t/swapchildren-referencing-the-items/264594>\
**Category:** flash\
**Created:** [June 27, 2008, 4:55pm UTC](https://forum.kirupa.com/t/swapchildren-referencing-the-items/264594 "2008-06-27T16:55:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![partimer](https://avatars.discourse-cdn.com/v4/letter/p/f1d935/32.png) [@partimer](https://forum.kirupa.com/u/partimer)\
**Post date:** [June 27, 2008, 4:55pm UTC](https://forum.kirupa.com/t/swapchildren-referencing-the-items/264594/1 "2008-06-27T16:55:00Z")

</div>

Ok - I know I’ve appeared here today with a few questions - my last of the day I promise!

My doc class (Main.as) calls and builds a few graphics (doBuild.as) and some text (mainText.as).

I’m trying to target the dynamic text with a listener (in the MainText.as) and swap the graphics when clicked - trouble being that I can’t seem to reference them back at all (i.e stage.swapChildren(doBuild.Navigation,doBuild.Navigation2);).  
Can anyone shed light on this?

Main.as

```auto
 
package {
 import flash.display.Sprite;
 import flash.display.MovieClip; 
 import flash.display.*
 public class Main extends Sprite {
  public function Main():void {
   var doBuild = new DoBuild(this);
   addChild(doBuild);
 
   var mainText = new MainText(this);
   addChild(mainText);
 
  }
 }
}

```

MainText.as

```auto
 
package {
 import flash.display.MovieClip; 
 import flash.text.*;
 import flash.events.*; 
 public class MainText extends MovieClip {
 
 public function MainText(stage:Main) { 
  var css:StyleSheet = new StyleSheet();
  var body:Object = new Object();
  body.fontFamily = "Arial";
  body.textIndent = 0;
  css.setStyle("body", body);
   var txtFld:TextField = new TextField();
   txtFld.x =830;
   txtFld.y =10;   
   txtFld.width = 100;
   txtFld.multiline = true;
   txtFld.wordWrap = true;
   txtFld.selectable = false;   
   txtFld.embedFonts = true;
   txtFld.autoSize = TextFieldAutoSize.LEFT;
   txtFld.styleSheet = css;
   txtFld.htmlText = "<body>";
   txtFld.htmlText += "<span class='heading'>Text</span>";
   txtFld.htmlText += "</body>";
   addChild(txtFld);
   txtFld.addEventListener(MouseEvent.CLICK, clickHandler);
    function clickHandler(event:Event):void
    {
     stage.swapChildren(doBuild.Navigation,doBuild.Navigation2);
 
    }   
}
}
}

```

doBuild.as

```auto
 
package {
 import flash.display.Sprite; 
 public class DoBuild extends Sprite {
 public function DoBuild(stage:Main) {
   var Navigation2:Sprite=new rightPanel2();
   addChild(Navigation2);
   trace("NAV2 = " + getChildIndex(Navigation2));
   Navigation2.x=832;
   Navigation2.y=2.4; 
   var Navigation:Sprite=new rightPanel();
   addChild(Navigation);
   trace("NAV = " + getChildIndex(Navigation));
   Navigation.x=832;
   Navigation.y=2.4;   
  } 
 }
}

```

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [June 27, 2008, 5:07pm UTC](https://forum.kirupa.com/t/swapchildren-referencing-the-items/264594/2 "2008-06-27T17:07:13Z")

</div>

Have it dispatch an event that the Main is listening for instead.

that way you keep any reference to it’s parent out of the class…

---

<div class="post-metadata">

**Author:** ![partimer](https://avatars.discourse-cdn.com/v4/letter/p/f1d935/32.png) [@partimer](https://forum.kirupa.com/u/partimer)\
**Post date:** [June 27, 2008, 8:02pm UTC](https://forum.kirupa.com/t/swapchildren-referencing-the-items/264594/3 "2008-06-27T20:02:28Z")

</div>

Thanks once again Sekasi, I’d figured it was a simple referencing error I was committing but this opens up a whole new bag of worms (and bugs) to me.

I’ll give it a bash and see where I get.

Really do appreciate your feedback here - many thanks once again!
