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

70 Chapter 3: Creating Advanced Components in Flash MX 2004
In some cases, you must call the invalidate() method after changing skin properties. This
depends on where you set the skin. If you set it prior to the
layoutChildren() method, the
invalidate() call in that method takes care of it for you. But if you set skin properties late in the
component instantiation life cycle, you might have to call the
invalidate() method again to
have Flash draw the new skins.
Adding styles
Adding styles is the process of registering all the graphic elements in your component with a class
and letting that class control the graphics at runtime. Flex gathers style information from style
sheets and external files, and builds a styleDeclaration object that stores this information.
When the component checks a style property, it queries its own style properties (and therefore the
properties of its ancestors). If it cannot find the style property, it checks to see if it has a
styleDeclaration object assigned to it.
If it does, it returns the corresponding property in the styleDeclaration object. If not, and the
property is not inherited, the value of the style is
undefined. If the property is inherited, it checks
the style property of its parent component. The parent component returns the property if it has it
or checks its parent. When the last parent is queried and no style has been found, Flex checks for
a global style object.
To set or get styles on an instance of an object, you access the UIObject
getStyle() or
setStyle() methods. The following example sets the color on a button instance:
myButton.setStyle("color", 0xFF00FF);
You cannot access style properties as ".propertyName". Instead, you must use the getStyle() or
setStyle() methods. This abstracts the code that handles inheriting styles and updating of a
component after a style change.
The
getStyle() and setStyle() methods have the following signatures:
getStyle(styleName:String);
setStyle(styleName:String, value):Void;
The getStyle() method returns a string, number, or object representing the styleName. It
returns
undefined if the style does not exist.
The
setStyle() method sets a style on the object (and children if it is a cascading style).
You do not need to implement code in your component to support styles and style inheritance,
because it is implemented in the base classes. For more information about styles, see Developing
Flex Applications.