When estente a class from other objects that it contains a MovieClip (MovieClip, TextField, etc ...) they may not be "ready" within the constructor. This happens especially when using more extended MovieClip or components (controls) of Flash. To solve the problem just use the onLoad event inherited from the class itself. For example imagine you have a MovieClip that contains a TextField object (static visual object inside flash) and a TextInput object (inserted control panel components) and want to set some property in the constructor of our class MyClass.
We create a symbol and insert inside either object TextField (let's call text_txt) is the TextInput object (textinput_txt). We associate this symbol to a class that extends MovieClip, call our class MyClass. What we get is that during the constructor of our class MyClass these two controls are initialized. If we try to use a code like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | / / controls . TextInput ; import mx. controls. TextInput; / / MovieClip { class MyClass extends MovieClip { / / text_txt : TextField ; private var text_txt: TextField ; textinput_txt : TextInput ; private var textinput_txt: TextInput; / / function MyClass () { "MiaClasse::costruttore" ) ; trace ("MyClass :: constructor"); / / = "Ciao" ; text_txt. text = "Hello"; = "Ciao" ; textinput_txt. text = "Hello"; / / } } |
What otteremo will only control the initialization of the TextField! If instead we try to use the onLoad event inherited from super class MovieClip:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | / / controls . TextInput ; import mx. controls. TextInput; / / MovieClip { class MyClass extends MovieClip { / / text_txt : TextField ; private var text_txt: TextField ; textinput_txt : TextInput ; private var textinput_txt: TextInput; / / function MyClass () { "MiaClasse::costruttore" ) ; trace ("MyClass :: constructor"); / / = "Ciao" ; text_txt. text = "Hello"; / / onLoad = _onLoad; } / / _onLoad ( ) { _onLoad private function () { = "Ciao" ; textinput_txt. text = "Hello"; } } |
In this case the string "Hello" will be visible on TextField that the TextInput. se preferite) utilizzata per eseguire tutte le inizializzazioni del caso, comprese quelle che normalmente inseriremo – con successo – nel costruttore. A general rule, then, when you have components and other MovieClip "derivatives", would be to always plug in the constructor a pointer to a private function _onLoad() (or init() if you prefer) used to perform all necessary initializations , including those that normally insert - successfully - in the constructor.










Obviously the same applies to the unload event ()
[...] The solution proposed in flash: how to initialize the components in the constructor of a class, for example, in ActionScript 3.0 it is this: PLAIN TEXT Actionscript: [...]