views:

634

answers:

2

I've created a small component in Flash CS4, and I have associated my MyComp symbol with it's respective MyComp class. The code in MyComp.as looks as follows:

package {
    import flash.display.MovieClip;

    public class MyComp extends MovieClip
    {
     public function MyComp()
     {
      trace(this.test);
     }

     private var _test:String;

     [Inspectable(defaultValue="blah")]
     public function get test():String
     {
      return this._test;
     }

     public function set test(v:String):void
     {
      this._test = v;
     }
    }
}

When I drag the component to a test FLA, the component's properties are all showing up as per the Inspectable[] meta tag. But when I set the properties in the Component Inspector, the value is always null, despite what the Component Inspector says.

When tracing for example test, it always outputs null?

How do I get the Component Inspector's values to reflect in the component at runtime?

+1  A: 

The order of operations with components and inspectable properties can be a bit tricky. Keith Peters (Bit-101) wrote up a nice overview of the problems with inspectable getters and setters.

The issue, in particular, is that the constructor gets called PRIOR to the inspectable properties being set. One nice way around this is to make your constructor setup a listener for the EXIT_FRAME event, which will run during the same frame, just after everything else is done. For example:

package {

    import flash.display.MovieClip;
    import flash.events.Event;

    public class SampleComponent extends MovieClip {

     private var _foo:Number;

     public function SampleComponent() {
      trace("SampleComponent: constructor");
      addEventListener(Event.EXIT_FRAME, onReady);
     }

     [Inspectable]
     public function get foo():Number {
         trace("SampleComponent: get foo: " + _foo);
      return _foo;
     }

     public function set foo(value:Number):void {
         trace("SampleComponent: set foo: " + value);
      _foo = value;
     }

     private function onReady(event:Event):void {
         trace("SampleComponent: ready!");
         removeEventListener(Event.EXIT_FRAME, onReady);
     }
    }
}
Branden Hall
Thanks for the link Branden! I've been pulling my hair out of my scalp for two weeks now, and can't get things working. I'll let know shortly if I managed to come across a solution.
Joe Zephyr
After reading the above link, I noticed my problem is due to me tracing in the constructor, before the component has finished initializing. I'll retry with an ADDED_TO_STAGE event or something likewise. Thanks for the post!
Joe Zephyr
Mmm. On ADDED_TO_STAGE, still null, thus still too early. Does Flash fire an event when the component has been created/initialized?
Joe Zephyr
Well, because this is a display object you can tap into the ENTER_FRAME event, but that would be fired a frame too late, so instead, try the new (to Flash 9) Event.EXIT_FRAME event. I'll post a code example in my answer in just a second.
Branden Hall
Most helpful, thanks Branden!
Joe Zephyr
I hate Flash Professional, yet I love ActionScript 3. :)
Joe Zephyr
A: 

It looks Event.EXIT_FRAME is Flash 10 only since I get undefined props error when it does compiled for Flash 9, contrary to the Doc....

Abusedmedia