User Guide
Table Of Contents
- Contents
- Working with Flash MX 2004
- Creating Basic Components in Flash MX 2004
- Creating Advanced Components in Flash MX 2004
- Contents
- About Creating components
- Writing the component’s ActionScript code
- Simple example of a class file
- General process for writing a class file
- Selecting a parent class
- Identifying the class, symbol, and owner names
- About the component instantiation life cycle
- Writing the constructor
- Specifying clip parameters
- Implementing the constructObject2() method
- Implementing the init() method
- Implementing the createChildren() method
- Implementing the commitProperties() method
- Implementing the measure() method
- Implementing the layoutChildren() method
- Implementing the draw() method
- Defining getters and setters
- Component metadata
- Defining component parameters
- Handling events
- Using the Event metadata
- About invalidation
- Skinning custom controls
- Adding styles
- Making components accessible
- Improving component usability
- Best practices when designing a component
- Using the ModalText example
- Troubleshooting
- Index

52 Chapter 3: Creating Advanced Components in Flash MX 2004
Writing the constructor
Generally, component constructors should be empty so that the object can be customized with its
properties interface. For example, the following code shows a constructor for MyComponent:
function MyComponent() {
}
In this example, when a new component is instantiated, the MyComponent() constructor is called.
Setting properties in the constructor can lead to overwriting default values, depending on the
ordering of initialization calls.
The empty constructor is not the only method necessary to instantiate the object with property
values. You can also override the
constructObject2() method. This happens because the
constructor is called too early in the life cycle of the object to support the proper construction of
it and its children. As a result, Flex finally instantiates the object when the
constructObject2()
method is called. For more information, see “Implementing the constructObject2() method”
on page 54.
Each class can contain only one constructor function; overloaded constructor functions are not
supported in ActionScript 2.0.
Specifying clip parameters
Clip parameters define the instance properties that you can set as an MXML tag’s property. The
values of these are stored in the initObject so that Flex can apply them to the component during
instantiation. Clip parameters mirror the set of properties in the Flash UI Property inspector.
They are used in Flex as a list of properties to be applied during the construction of the object.
Clip parameters are roughly equivalent to constructor parameters in Java or C++, although they
are not set in the constructor call, but later, in the
constructObject2() method.
Clip parameters are not meant to set default values, but rather to apply tag attributes to the
component when it is created.
You define clip parameters and then apply them to the object using the
UIObject.applyProperties() method inside the constructObject2() method. You use the
applyProperties() method to apply the clip parameters, based on values set by the user in the
MXML tag.
When playing a standard Flash SWF file, Flash Player applies clip parameters before the object’s
constructor is called (which calls the
constructObject() method, which calls the init()
method).
In Flex, Flash Player no longer applies clip parameters by default. This lets you control when they
are applied, which should be in the
constructObject2() method. If you do not apply the clip
parameters explicitly in the
constructObject2() method, Flex applies them after the
constructor finishes.
In an MXML file, you might have a tag that defines a foo property, as the following
example shows:
<MyComponent foo="myvalue" />