User Guide
164 Creating Components
About the constructor function
You can recognize a constructor function because it has the same name as the component
class. For example, the following code shows the ScrollBar component’s constructor function:
function ScrollBar() {
}
In this case, when a new scroll bar is instantiated, the ScrollBar() constructor is called.
Generally, component constructors should be empty. Setting properties in constructors can
sometimes lead to overwriting default values, depending on the order of initialization calls.
If your component extends UIComponent or UIObject, Flash automatically calls
init(),
createChildren(), and size() methods and you can leave your constructor function
empty, as shown here:
class MyComponent extends UIComponent{
...
// this is the constructor function
function MyComponent(){
}
}
All version 2 components should define an init() function that is called after the constructor
has been called. You should place the initialization code in the component’s
init() function.
For more information, see the next section.
If your component extends MovieClip, you may want to call an
init() method, a
createChildren() method, and a method that lays out your component from the
constructor function, as shown in the following code example:
class MyComponent extends MovieClip{
...
function MyComponent(){
init()
}
function init():Void{
createChildren();
layout();
}
...
}
For more information about constructors, see “Writing the constructor function” in Learning
ActionScript 2.0 in Flash.