3

When moving to Flash CC, the following problem occurred in my flash project.

Consider this class definition:

public class Test extends MovieClip {
  [Inspectable(type="String", defaultValue="val")]
  public var param :String;

  public function Test() {
     trace(param);
  }
}   

I have a symbol "Symbol 1" which (via the Library panels Properties) is linked to the class Test and (via the Component Definition) is also set to the class Test, and this dialog box displays the parameter "param" with value "val". I have an instance of Symbol 1 on the Scene. The parameter "param" appears in the properties of this instance, with the value "val", as expected.

The only problem is that during runtime, the value of the parameter "param" is equal to null, as confirmed during the execution of the classes constructor, which outputs "null".

Does anyone know why this is happening?

szymtor
  • 167
  • 1
  • 7

1 Answers1

3

The Inspectable tag is needed by Flash to populate the component properties panel in order to set values manually. These parameters, both default and user input, are not available at instantiation, but they are available only at the following frame. In order to have default values at instantiation you must set the default value also on the variable itself.

  [Inspectable(type="String", defaultValue="val")]
  public var param :String = "val";

Also, before you go crazy accessing values inserted with property inspector, remember to add an enter frame event before accessing those values.

What I usually do in my components: 1 - Populate default in both inspectable and variable 2 - On instantiation, if a parameters object is received, then I know it's instantiated in code and values are inside the parameters object 3 - If a parameters object is not received, then instantiation is done on timeline visually, therefore I access properties on next frame

FilippoG
  • 546
  • 3
  • 16
  • Thanks. I had no idea that the value is assigned only in the following frame. Is there any reasonable explanation for this behaviour? – szymtor Sep 15 '15 at 14:03
  • 1
    Yes there is a logical explanation, not really reasonable :) but logical. Since properties are defined on the instance itself, probably execution stack instantiates component itself first, and then grabs values from properties. In fact generally in Adobe components properties relate to setters, not to variables, therefore once they are grabbed some code is executed. It took me a while to figure this out. I guess you too write components without extending the UIComponent class right? – FilippoG Sep 16 '15 at 12:37
  • Yes, I don't extend UIComponent. Would that help in this situation? Unfortunately I cannot populate neither the inspectable nor the variable in the component class, as I don't want to modify the class – I want to set the inspectable value of a particular instance of the class on the stage automatically, using JSFL. – szymtor Feb 19 '16 at 09:39
  • Sorry for the late reply. I guess extending UIComponent logically wouldn't help, but I have never tried therefore I can't really say. – FilippoG Apr 17 '16 at 20:52
  • BTW, I did spot another glitch in components. If anywahere in the component definition code there is "Vector.<*>". I mean, anywhere, as variable type, or instantiated, literally anywhere. If that sentence is in code, then changes in component definition will not be reflected in component inspector!!! Isn't it weird? It did drive me crazy, don't ask me how I got there :) – FilippoG Apr 17 '16 at 20:54