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

32 Chapter 2: Creating Basic Components in Flash MX 2004
Add the ChangeEvent metadata keyword to the getter, and a dispatchEvent() method call
inside the setter. When the property’s value is set, the component dispatches the
change event.
Since the getter is bound to the ChangeEvent
change, the binding subsystem knows what to
listen for when that property changes.
At the top of the class file, you must also add the Event metadata keyword to identify
change as
an event that this component emits.
The following ActionScript class file shows how to bind a property to an event:
[Event("change")]
class BindChar extends mx.core.UIComponent {
static var symbolName:String="BindChar";
static var symbolOwner:Object = BindChar;
var className:String = "BindChar";
function BindChar() {
}
// This example uses a TextField to store and display the character.
var myCharField:TextField;
function init() {
super.init();
tabEnabled = true;
invalidate();
}
function keyDown(evt:Object):Void {
//Triggers the setter on every keystroke.
char = String.fromCharCode(evt.ascii);
}
[ChangeEvent("change")]
public function get char():String {
return myCharField.text;
}
public function set char(c:String) {
myCharField.text = c;
dispatchEvent({ type: "change" });
}
}
The following MXML file binds the component’s char property to the TextArea control’s
text property:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*" >
<BindChar id="myComp1" char="F" />
<mx:TextArea id="ta1" text="{myComp1.char}" />
</mx:Application>