title: Getting a named child of a MovieClip from an external swf
body: <p>I'm working on a flash project in which I have a swf with a bunch of art assets. However, they are not on the stage, there are simply in the library, and linked up to code I have written.</p>
  
  <p>I have one class that I would like to instantiate, but I keep getting a reference error when it's being constructed.</p>
  
  <p>Here's how the swf/fla Library is set up  </p>
  
  <pre><code>[MovieClip]ButtonPopup, Linkage:com.packagename.ButtonPopup
  [MovieClip]popup_btn, Linkage: none
  </code></pre>
  
  <p>popup_btn is a child of Popup, and aptly named _popup_btn (with a getter generated by Flash Builder called popup_btn) within the Flash CS5.5 IDE. Here's the Popup class:  </p>
  
  <pre><code>package com.packagename.ButtonPopup {
  
      public class ButtonPopup extends Popup {
          public Popup() {
              addClickListeners(popup_btn);
          }
  
          protected function get popup_btn():MovieClip {
              return this["_popup_btn"];
          }
      }
  }
  
  package com.packagename.Popup {
      public class Popup extends MovieClip {
          public Popup() {
  
          }
  
          protected function addClickListeners(mc:MovieClip) {
              //add click listeners
          }
      }
  }
  </code></pre>
  
  <p>My main swf looks like so:  </p>
  
  <pre><code>public MainClass() {
      var loader:Loader = new Loader();
      loader.load(new URLRequest("assets.swf"));
      loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadArtComplete);
  }
  
  function loadArtComplete (e:Event):void {
      var popup_class:Class = e.target.applicationDomain.getDefinition("com.packagename::Popup");
      var popup_mc:MovieClip = new popup_class() as MovieClip;
  
      addChild(popup_mc);
  }
  </code></pre>
  
  <p>Sadly, I get an error on the <code>var popup_mc:MovieClip = new popup_class() as MovieClip</code> line:</p>
  
  <pre><code>ReferenceError: Error #1056: Cannot create property _popup_btn on com.packagename.Popup
      at flash.display::Sprite/constructChildren()
      at flash.display::Sprite()
      at flash.display::MovieClip()
      at com.packageName.Popup() [file path]
      at com.packageName.MainClass/loadArtComplete() [filepath]
  </code></pre>
  
  <p>If I leave the popup_btn unnamed in the Flash IDE, I don't get an error, but then I can't reference the MovieClip (for adding click listeners and the like), so that's a no-go solution.</p>
  
  <p>Does anyone have any ideas on what to do here? Can I not instantiate a "complex" MovieClip but instead have to instantiate each child MovieClip, one by one?</p>
  
  <p>EDIT: After a bit more testing, it looks like it has to do with the fact that the problem class isn't inheriting directly from MovieClip, but from another class (which is inheriting from MovieClip). I updated the source code to reflect that. I still don't know if that's the case (if not inheriting from MovieClip would cause a problem). But we'll see.</p>
  
  <p>Thanks,<br>
  -Esa</p>