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

Setting default sizes 37
function init() {
super.init();
tabEnabled = true;
invalidate();
}
function keyDown(Void):Void {
var k = Key.getCode();
trace("key: " + k);
dispatchEvent({type:"mykeydown", myKey:k});
}
}
The following MXML file handles the keyboard event that the class file emits. The MXML file
inspects the event object’s
myKey property to determine which key the user pressed.
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*">
<mx:Script>
<![CDATA[
function handleKeyDown(evt) {
if (evt.myKey == 8) { // backspace
ta1.text = "";
} else {
ta1.text='pressed: ' + evt.myKey;
}
}
]]>
</mx:Script>
<greensquare id="myGS" mykeydown="handleKeyDown(event);" />
<mx:TextArea id="ta1" text="" />
</mx:Application>
Setting default sizes
You can set the default size of a custom component by setting the values of the
_measuredPreferredWidth and _measuredPreferredHeight properties in the measure()
method. These values will be used if no explicit width and height are specified in the component’s
MXML tag. Your measure function should not set the
preferredWidth and preferredHeight
properties.
The following class sets the default size of the BlueButton control to 500 by 200 pixels, and uses
the
setStyle() method to define the default fontSize property for the button’s label:
class BlueButton extends mx.controls.Button {
static var symbolName:String="BlueButton";
static var symbolOwner:Object = BlueButton;
var className:String = "BlueButton";
function BlueButton() {