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

68 Chapter 3: Creating Advanced Components in Flash MX 2004
The event object has an implicit property, target, that is a reference to the object that triggered
the event.
For more information on events, see Developing Flex Applications.
Defining event handlers
You define the event handler object or event handler function that listens for your component’s
events in your application’s ActionScript. The following example handles the
change, focusOut,
and
click events of the children of the component:
function handleEvent(evt:Object):Void {
if (evt.type == "change") {
dispatchEvent({ type: "change" });
} else if (evt.type == "focusOut") {
text_mc.editable = false;
} else if (evt.type = "click") {
text_mc.editable = !text_mc.editable;
}
}
Using the Event metadata
Add Event metadata in your ActionScript class file for each event listener. The value of the
Event
keyword becomes the first argument in calls to the
addEventListener() method, as the
following example shows:
[Event("click")] // Event declaration.
...
class FCheckBox{
function addEventListener(eventName:String, eventHandler:Object) {
... // eventName is String
}
}
Event metadata describes the events that this component emits, not the ones it consumes. For
more information on the
Event metadata keyword, see “Event” on page 62.
About invalidation
Macromedia recommends that a component not update itself immediately in most cases, but
instead save a copy of the new property value, set a flag indicating what is changed, and call one of
the three invalidation methods.
The following are the invalidation methods:
invalidateSize() Indicates that one of the _measured properties may have changed. This results
in a call to the
measure() method. If the measure() method changes the value of one of the
_measured properties, it calls the layoutChildren() method.
invalidateLayout() Indicates that the position and/or size of one of the subobjects may have
changed, but the
_measured properties have not been affected. This results in a call to the
layoutChildren() method. If you change a subobject in the layoutChildren() method, you
should call the
invalidate() method.