MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH Manual page 269

Learning actionscript 2.0 in flash
Table of Contents

Advertisement

Consider the following points when you write constructor functions:
If no constructor function is explicitly declared—that is, if you don't create a function
whose name matches that of the class—the compiler automatically creates an empty
constructor function for you.
A class can contain only one constructor function; overloaded constructor functions are
not allowed in ActionScript 2.0.
A constructor function should have no return type.
The term constructor is also typically used when you create (instantiate) an object based on a
particular class. The following statements are calls to the constructor functions for the top-
level Array class and the custom Person class:
var day_array:Array = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
"Sat");
var somePerson:Person = new Person("Tom", 30);
Next you'll add a special function called a constructor function.
The following exercise is part of
do not wish to progress through the example, you can download the class files from
www.helpexamples.com/flash/learnas/classes/.
To add the constructor functions to your class files:
1.
Open the ClassA.as class file in the Flash authoring tool.
2.
Modify the existing class file so it matches the following code (the changes to make appear
in boldface):
class com.macromedia.utils.ClassA {
function ClassA() {
trace("ClassA constructor");
}
}
The previous code defines a constructor method for the ClassA class. This constructor
traces a simple string to the Output panel, which will let you know when a new instance
of the class has been created.
3.
Open the ClassB.as class file in the Flash authoring tool.
4.
Modify the class file so it matches the following code (the changes to make appear
in boldface):
class com.macromedia.utils.ClassB {
function ClassB() {
trace("ClassB constructor");
}
}
"Example: Writing custom classes" on page
Example: Writing custom classes
263. If you
269

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?

This manual is also suitable for:

Flash 8

Table of Contents