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

30 Chapter 2: Creating Basic Components in Flash MX 2004
23.
Copy the SWC file that you created (BlueButton.swc) to the same directory as the MXML file.
This should not be the same directory into which you saved the ActionScript and FLA file. This
directory must be in the web application’s directory structure so that Flex can compile a SWF
from the MXML file.
24.
Request the MXML file in your browser or a stand-alone Flash Player. The blue button should
appear.
Working with component properties
Most components have instance properties that the Flex author can set while adding the
component to the application. To make this possible, you add properties to the class definition.
You can set all component properties as properties in MXML and in ActionScript, unless they are
explicitly marked private.
You can expose component properties by doing the following:
• Creating an instance variable
• Defining getters and setters
These methods are described in the following sections.
Creating an instance variable
Creating instance variables is simple. In the component’s ActionScript class file, you declare a
variable:
var myName:String;
If the variable declaration does not specify a default value, your code must either take into
account that myName might be
undefined, or the user could encounter unexpected results.
In MXML, you set that property using a tag property:
<MyComponent myName="Ted" />
As a result, you should define a default value for most regular properties, as the following example
shows:
var myName:String = "Fred";
You can use the Inspectable keyword to set the default value of a property and to limit the
available values for the property; for example:
[Inspectable(defaultValue="left", enumeration="left, right")]
function set labelPlacement(p:String)
...
In this example, the labelPlacement property is limited to the values left or right, and the
default value is
left. For more information on using the Inspectable keyword, see “Inspectable”
on page 63. The ModalText example class file shows the use of the Inspectable keyword. For more
information, see “Using the ModalText example” on page 73.