MACROMEDIA FLASH MX 2004-LEARNING FLASH Manual page 116

Learning flash
Table of Contents

Advertisement

In order to properly define a class in ActionScript 2.0, you must surround all classes by the
keyword, and you must declare all variables in the constructor outside of the constructor.
Following is an example:
Note: The following ActionScript is an example only. You should not enter the script in your lesson
FLA file.
class Product
{
//variable declaration
var id:Number
var productName:String
var price:Number
//constructor
function Product (id:Number, prodName:Name, price:Number)
{
this.id
= id;
this.prodName = prodName;
this.price = price;
}
}
To create objects from this class, you could now use the following code:
Note: The following ActionScript is an example only. You should not enter the script in your lesson
FLA file.
var cliplessPedal:Product=new Product(1, "Clipless Pedal", 11);
var monkeyBar:Product=new Product(2, "Monkey Bar", 10);
However, in ActionScript 2.0, variables that are part of a class structure should not be accessed
directly. You should write methods within the class that will access these variables directly. There
should be different methods that get and set properties (known as "getter" and "setter" methods).
You must indicate the data type for both a method's return value and any parameters that are
passed to the method when it is called.
Specify the data type for method return values
You must indicate data types for values returned by methods after the method name and list of
parameters, as in the following example:
Note: The following ActionScript is an example only. You should not enter the script in your lesson
FLA file.
public function getProductName() :String
{
return name;
}
If no value is returned (for example, a property is being set), the data type is
public function setProductName(productName:String) :Void
{
this.productName=productName;
}
116
Chapter 15: Work with Objects and Classes Using ActionScript 2.0
class
:
Void

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH MX 2004-LEARNING FLASH and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Flash mx 2004 - actionscript

Table of Contents