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

34 Chapter 2: Creating Basic Components in Flash MX 2004
Because nearly all classes extend the UIObject class, the initialize event is already supported in
custom components. To define a handler for it, you add the
initialize property to the
component’s MXML tag, and then add ActionScript code that processes the event, as the
following example shows:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*">
<mx:Script>
<![CDATA[
function myInit() {
trace('init greensquare');
}
]]>
</mx:Script>
<greensquare initialize="myInit();" />
</mx:Application>
Handling mouse events
All visual components that inherit from the UIObject class support a number of navigational
mouse events, including the following:
• mouseOver
• mouseOut
• mouseDown
• mouseUp
These events do not include the
click event. For a complete list of events supported by visual
components, see the information about the control in Developing Flex Applications.
To use the common mouse events, you point to a handler in your MXML file. You do not need
to add any additional code to the component source code.
The following example changes the alpha (transparency) of the Green Square component
when the mouse moves over the component, and again when the mouse moves away from
the component:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*">
<mx:Script>
<![CDATA[
var startAlpha:Number = 40;
function myInit() {
myGS.alpha=startAlpha;
}
function changeAlpha(curAlpha:Number) {
myGS.alpha=curAlpha;
}