MACROMEDIA FLEX-CREATING ADVANCED COMPONENTS Manual - page 13
Writing the component’s ActionScript code
13
Inside the
draw()
method, you can use calls to the Flash drawing API, defined by the MovieClip
class, to draw borders, rules, and other graphical elements. You can also call the
clear()
method,
which removes the visible objects. In general, to set lengths in the
draw()
method, you should use
this.layoutWidth
and
this.layoutHeight
properties instead of
width
and
height
. For more
information on the drawing API, see the MovieClip class in Flex ActionScript Language Reference.
The following example clears the component and then draws a border around the component:
function draw():Void {
clear();
if (bTextChanged) {
bTextChanged = false;
text_mc.text = text;
}
// Draw a border around everything.
drawRect(0, 0, this.layoutWidth, this.layoutHeight);
}
Defining getters and setters
Getters and setters provide visibility to component properties and control access to those
properties by other objects.
To define getter and setter methods, precede the method name with
get
or
set
, followed by a
space and the property name. Macromedia recommends that you use initial capital letters for the
second word and each word that follows. For example:
public function get initialColorStyle(): Number
The variable that stores the property’s value cannot have the same name as the getter or setter. By
convention, precede the name of the getter and setter variables with two underscores (__). In
addition, Macromedia recommends that you declare the variable as private.
The following example shows the declaration of
__initialColor
, and getter and setter methods
that get and set the value of this property:
...
private var __initialColor:Color = 42;
...
public function get initialColor():Number {
return __initialColor;
}
public function set initialColor(newColor:Number) {
__initialColor = newColor;
}
You commonly use getters and setters in conjunction with metadata keywords to define properties
that are visible, bindable, and have other properties.
Defining MXML Properties
Your custom components are usable in MXML files. For example, if you define a component in
the class called MyComponent in the ActionScript file MyComponent.as, you can reference it in
MXML as the following example shows: