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

36 Chapter 2: Creating Basic Components in Flash MX 2004
}
}
The following MXML file handles the click event within an <mx:Script> block:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*"
initialize="createListener();" >
<mx:Script>
<![CDATA[
var startAlpha:Number = 40;
var curState:Number = 100;
function myInit() {
myGS.alpha=startAlpha;
}
function modAlpha(curAlpha:Number) {
gs.alpha=curAlpha;
if (curState==100) {
curState=40;
} else {
curState=100;
}
}
]]>
</mx:Script>
<greensquare id="gs" initialize="myInit();" click="modAlpha(curState);" />
</mx:Application>
For an example of a custom component that emits and handles its events, see “Creating
compound components” on page 42.
Handling keyboard events
Keyboard events are emitted by all components that extend the UIComponent class. To make use
of keyboard events in your MXML files, you should capture the keys and include the value of the
key in your event object, and then dispatch the keyboard event with the event object.
The following component class emits the
keyDown event so that the Flex application can handle
it. It builds an event object, adding the ASCII value of the key that the user pressed as the
myKey
property of the event object. The class extends the UIComponent class but not the UIObject
class, which does not handle this event.
[Event("mykeydown")]
class greensquare extends mx.core.UIComponent {
static var symbolName:String="greensquare";
static var symbolOwner:Object = greensquare;
var className:String = "greensquare";
function greensquare() {
}