Flash CS3 Documentation |
|||
| Using ActionScript 2.0 Components > Creating Components > Creating the ActionScript class file > Using getter/setter methods to define parameters | |||
The simplest way to define a component parameter is to add a public member variable that makes the parameter "inspectable." You can do this by using the Inspectable tag in the Component inspector, or adding the Inspectable variable as follows:
[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.
To define getter/setter methods, remember these points:
get or set, followed by a space and the property name.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, see Adding component metadata.
For more information about getter/setter methods, see About getter and setter methods in Learning ActionScript 2.0 in Adobe Flash.
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00002495.html