Flash: how to initialize the components in the constructor of a class

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.

Example 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.

2 comments to "Flash: how to initialize the components in the constructor of a class"

  1. November 7, 2007 Giovambattista Fazioli :

    Obviously the same applies to the unload event () ;)

  2. December 19, 2007 undolog »Blog Archive» Flash CS3: The new event handling :

    [...] 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: [...]

Leave a comment

TAG XHTML PERMITS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA