User Guide

Creating the ActionScript class file 935
Using getter/setter methods to define parameters
The simplest way to define a component parameter is to add a public member variable that is
Inspectable:
[Inspectable(defaultValue="strawberry")]
public var flavorStr:String;
However, if code that employs a component modifies the flavorStr property, the component
typically must perform an action to update itself in response to the property change. For example,
if
flavorStr is set to "cherry", the component might redraw itself with a cherry image instead
of the default strawberry image.
For regular member variables, the component is not automatically notified that the member
variable’s value has changed.
Getter/setter methods are a straightforward way to detect changes to component properties.
Instead of declaring a regular variable with
var, declare getter/setter methods, as follows:
private var __flavorStr:String = "strawberry";
[Inspectable(defaultValue="strawberry")]
public function get flavorStr():String{
return __flavorStr;
}
public function set flavorStr(newFlavor:String) {
__flavorStr = newFlavor;
invalidate();
}
The invalidate() call causes the component to redraw itself with the new flavor. This is the
benefit of using getter/setter methods for the
flavorStr property, instead of a regular member
variable. See “Defining the draw() method” on page 947.
To define getter/setter methods, remember these points:
Precede the method name with get or set, followed by a space and the property name.
The variable that stores the propertys value cannot have the same name as the getter or setter.
By convention, precede the names of the getter and setter variables with two underscores.
Getters and setters are commonly used in conjunction with tags to define properties that are
visible in the Property and Component inspectors.
For more information about getters and setters, see “Implicit getter/setter methods” in Using
ActionScript in Flash.
Adding component metadata
You can add component metadata tags in your external ActionScript class files to tell the compiler
about component parameters, data binding properties, and events. Metadata tags are used in the
Flash authoring environment for a variety of purposes.
The metadata tags can only be used in external ActionScript class files. You cannot use metadata
tags in FLA files.