Flash CS4 - component inspector, properties always vanish?

I created a small component in Flash CS4, and I linked my MyComp character to its corresponding MyComp class. The code in MyComp.as is 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 a component into a test FLA, all the properties of the component are displayed according to the Inspectable [] meta tag. But when I set the properties in the component inspector, the value is always zero, regardless of what the component inspector says.

When tracing, for example, test, does it always print null?

How do I get the Component Inspector values ​​in a component at runtime?

+4
source share
2 answers

The order of operations with components and verifiable properties can be a bit complicated. Keith Peters (Bit-101) wrote a good overview of problems with verified getters and setters .

The problem, in particular, is that the constructor gets the name PRIOR for settable checked properties. One good way to do this is for your constructor to set a listener for an EXIT_FRAME event that will run for the same frame immediately after everything else. For instance:

 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); } } } 
+4
source

You can simply use the text string "exitFrame", for example:

 addEventListener( "exitFrame", onExitFrame ); 

The event is fired, it seems that the Event class simply does not have a definition.

+1
source

All Articles