Creating a custom class
117
Build a custom class
You’ll now build a new Product class with getter and setter methods and create an object from the
Product class.
1.
Create an ActionScript file by doing one of the following:
■
If you’re using Flash MX 2004 Professional, select File > New > ActionScript File (Not Flash
Document). Save the document with the name Product.
■
If you’re using Flash MX 2004, open a text editor, such as Notepad. Save the file
with the name Product.as. (Remember to give the file the AS extension, to create an
ActionScript file.)
2.
Create a constructor for a Product class by creating a function called Product that takes the
arguments
id
,
prodName
, and
description
:
function Product (id:Number, prodName:String, description:String)
{}
3.
In the constructor function, set the properties of the Product class equal to the setter methods
that you will create:
setID(id);
setProdName(prodName);
setDescription(description);
4.
Wrap the constructor function around the
class
keyword. Be sure to declare each variable used
in the class:
class Product
{
var id:Number;
var prodName:String;
var description:String
function Product (id:Number, prodName:String, description:String)
{
setID(id);
setProdName(prodName);
setDescription(description);
}
}
5.
Define getter and setter methods for each property of the class, as in the following example. Be
sure to specify
Void
as the return type for the setter methods, and indicate the data type returned
for the getter methods.
class Product
{
var id:Number;
var prodName:String;
var description:String
function Product (id:Number, prodName:String, description:String) {
setID(id);
setProdName(prodName);
setDescription(description);
Summary of Contents for FLASH MX 2004-LEARNING FLASH
Page 1: ...Learning Flash...
Page 8: ...8 Contents...
Page 34: ...34 Chapter 3 Write Scripts with ActionScript...
Page 54: ...54 Chapter 6 Create a User Interface with Layout Tools...
Page 62: ...62 Chapter 7 Draw in Flash...
Page 68: ...68 Chapter 8 Create Symbols and Instances...
Page 76: ...76 Chapter 9 Add Animation and Navigation to Buttons...
Page 104: ...104 Chapter 13 Add Interactivity with ActionScript...
Page 112: ...112 Chapter 14 Create a Form with Conditional Logic and Send Data...
Page 122: ...122 Chapter 15 Work with Objects and Classes Using ActionScript 2 0...