User Guide

Creating the ActionScript class file 165
Defining the draw() method
You can write code in the draw() method to create or modify visual elements of a
component. In other words, in the
draw() method, a component draws itself to match its
state variables. Since the last
draw() method was called, multiple properties or methods may
have been called, and you should try to account for all of them in the body of
draw().
However, you should not call the
draw() method directly. Instead, call the invalidate()
method so that calls to
draw() can be queued and handled in a batch. This approach
increases efficiency and centralizes code. (For more information, see About invalidation
on page 166.)
Inside the
draw() method, you can use calls to the Flash drawing API to draw borders, rules,
and other graphical elements. You can also set property values and call methods. You can also
call the
clear() method, which removes the visible objects.
In the following example from the Dial component (see “Building your first component”
on page 127), the
draw() method sets the rotation of the needle to the value property:
function draw():Void {
super.draw();
dial.needle._rotation = value;
}
Defining the size() method
When a component is resized at runtime using the componentInstance.setSize() method,
the
size() function is invoked and passed width and height properties. You can use the
size() method in the components class file to lay out the contents of the component.
At a minimum, the
size() method should call the superclasss size() method
(
super.size()).
In the following example from the Dial component (see “Building your first component”
on page 127), the
size() method uses the width and height parameters to resize the dial
movie clip:
function size():Void {
super.size();
dial._width = width;
dial._height = height;
invalidate();
}
Call the invalidate() method inside the size() method to tag the component for redraw
instead of calling the
draw() method directly. For more information, see the next section.