Search code examples
flashactionscript-3flash-cs3

Flash AS3 : addChild() does not display imported movieclip


I fought with the IDE and read around all day enough to learn that in AS3, the exported class name of a lib symbol matters.

I have hello.fla. In it I created a simple textfield with a string ('hello') - coverted it to a symbol (movieclip) and did the following:

  • made the classname 'Hello'.
  • Export for AS is checked
  • Export in first frame is checked.

Once i did all that i nuked the instance on the stage. I thought I might add some extra functionality later so I actually also built a Hello.as class, which extends MovieClip, and which lives in the default pkg* and the whole fla builds fine:

package 
{
    import flash.display.MovieClip;

    public class Hello extends MovieClip
    {
        public function Hello()
        {
        }
    }
}

Now my main.fla, same folder, uses document class Main, and Main.as does the following:

private var h:MovieClip;
//...
h = new Hello();
this.addChild(h); //no joy

**till i get this working, nothing is in folders: all the files are in the root folder.*


Solution

  • THE SOLUTION

    Suppose that the library symbol lives inside an .fla such as Hello.fla:

    package 
    {
        import flash.display.MovieClip;
        import flash.text.TextField;
    
        /**
         * empty mc (blank stage) with a single library symbol containing whatever the hell you want (eg a shape). with settings:
         * class = Hello <=== this refers to a specific library symbol, not the entire Hello.fla and whatever else is on the stage when you build it.
         * export for AS : checked
         * export for runtime sharing (as Hello.swf) : checked <==== This was the step I'd missed
         * export in 1st frame : checked
         */
        public class Hello extends MovieClip
        {
            public function Hello()
            {
            }
        }
    }
    

    The main timeline / document class :

    package 
    {
        import flash.display.Loader;
        import flash.display.MovieClip;
        import flash.net.URLRequest;
        import flash.events.Event;
        import Hello;
    
        /**
         * this is the document class.
         */
        public class Main extends MovieClip
        {
            public function Main()
            {
                this.h = new Hello();
                this.l= new Loader();
                this.l.load( new URLRequest("Hello.swf") );
                this.l.contentLoaderInfo.addEventListener(Event.COMPLETE, this.test);
            }
    
            public function test(e:Event)
            {
                this.h = new Hello();
                h.x = 100;
                h.y = 100;
                this.addChild(this.h); //shows up on stage. Finally!
            }
    
            private var h:MovieClip;
            private var l:Loader;
        }
    
    }
    

    Hope it helps some other newbs like me new to AS3.