views:

1738

answers:

3

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.

A: 

Instead of this.addChild(h), does it work if you try root.addChild(h)?

Edit: _root -> root for AS3

Parappa
that is AS2 code. In Actionscript 3, _root becomes root
Dasuraga
sorry that didn't work...
rekindleMyLoveOf
using root yields error 1061.
rekindleMyLoveOf
Wrong. There is no root in AS3. You can "emulate it", but it not a language magic variable as it used to be.
Seth Illgard
A: 

you need to add it to the stage, ie

stage.addChild(h);
Dasuraga
well 'stage' did nothing, so I tried 'Stage', and I get access of undefined property Stage. :(
rekindleMyLoveOf
I also tried import flash.display.Stage, which leads to a different error (#1061 - Call to a possibly undefined method addChild through a reference with static type Class).
rekindleMyLoveOf
+1  A: 

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.

rekindleMyLoveOf