Flash CS3 Documentation |
|||
| Using ActionScript 2.0 Components > Creating Components > Creating the ActionScript class file > About core functions > 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.)
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), the draw() method sets the rotation of the needle to the value property:
function draw():Void {
super.draw();
dial.needle._rotation = value;
}
Flash CS3