To indicate that objects can add and access dynamic properties at runtime, precede the class
statement with the
keyword. To create subclasses of a class, use the
implements
extend only one class, but can implement several interfaces.) You can use
in a single statement. The following examples show typical uses of the
extends
keywords:
extends
class C implements Interface_i, Interface_j
class C extends Class_d implements Interface_i, Interface_j
class C extends Class_d, Class_e
For more information, see in Using ActionScript in Flash.
Example
The following example creates a class called Plant. The Plant constructor takes two parameters.
// Filename Plant.as
class Plant {
// Define property names and types
var leafType:String;
var bloomSeason:String;
// Following line is constructor
// because it has the same name as the class
function Plant(param_leafType:String, param_bloomSeason:String) {
// Assign passed values to properties when new Plant object is created
this.leafType = param_leafType;
this.bloomSeason = param_bloomSeason;
}
// Create methods to return property values, because best practice
// recommends against directly referencing a property of a class
function getLeafType():String {
return leafType;
}
function getBloomSeason():String {
return bloomSeason;
}
}
In an external script file or in the Actions panel, use the
var pineTree:Plant = new Plant("Evergreen", "N/A");
// Confirm parameters were passed correctly
trace(pineTree.getLeafType());
trace(pineTree.getBloomSeason());
The following example creates a class called ImageLoader. The
three parameters.
// Filename ImageLoader.as
class ImageLoader extends MovieClip {
function ImageLoader(image:String, target_mc:MovieClip, init:Object) {
var listenerObject:Object = new Object();
listenerObject.onLoadInit = function(target) {
for (var i in init) {
target[i] = init[i];
}
204
Chapter 2: ActionScript Language Reference
keyword. To declare that a class implements an interface, use the
dynamic
extends
// OK
// not OK
operator to create a Plant object.
new
ImageLoader
keyword. (A class can
and
implements
implements
// OK
constructor takes
and
Need help?
Do you have a question about the FLASH MX 2004-ACTIONSCRIPT LANGUAGE and is the answer not in the manual?
Questions and answers