Search code examples
actionscript-3document-class

Reference from the DocumentClass a movieclip created by an instance of another class


I have this main (document class) in a Flash project:

package  {
    import flash.display.MovieClip;
    import flash.events.Event;
    public class main extends MovieClip {

        public function main() {
            var other=new Other(this);
        }
    }   
}

This one is Other class:

    package  {
        import flash.display.MovieClip;
        import Clippo;

        public class Other extends MovieClip {
            //
            public function Other(ref) {
                //
                var clippo = new Clippo();
                clippo.name="clippo";
                clippo.x=100;
                clippo.y=100;
                //1
                //ref.addChild(clippo);
                //2
                addChild(clippo);
            }
        }
}

Now: if I pass a reference (ref) of the main class to Other and I add clippo as you can see in the first case, I can reference the movieclip clippo from the main (getChildAt(0) is "clippo" from the main). But, is there any way to use the second method (no ref) and do the same from the main class? I can see clippo onstage when Other creates it but I can't understand where clippo "lives" into the DisplayList.


Solution

  • I'm not exactly sure what you're trying to accomplish, but:

    1. You need to add other to the display list if you want to see it or its children.

      public function main()
      {
          var other:Other = new Other(this);
          addChild(other);
      }
      
    2. You can use root instead of passing a reference to the document class. (Once it's on the display list).

      root.addChild(clippo);
      

      But, it makes more sense to just add it to Other: addChild(clippo)

    3. Where you create DisplayObjects doesn't affect the display list, only calling addChild does. Calling addChild in the document class, and root.addChild in Other result in the same display list.