Adobe Flash

Create a custom class

Although ActionScript includes many classes of objects, such as the MovieClip class and the Color class, there will be times when you need to construct your own classes so you can create objects based on a particular set of properties or methods.

To create a class that defines each of the new objects, you create a constructor for a custom object class and then create new object instances based on that new class, as in the following example:

Note

The following ActionScript is an example only. Do not enter the script in your lesson FLA file.


function Product (id:Number, prodName:String, price:Number)
{

  this.id = id;
  this.prodName = prodName;
  this.price = price;

}

To properly define a class in ActionScript 2.0, you must surround all classes by the class keyword, and you must declare all variables in the constructor outside of the constructor.

Note

The following ActionScript is an example only. Do not enter the script in your lesson FLA file.


class Product
{

  // variable declarations
  var id:Number
  var prodName:String
  var price:Number


  // constructor
  function Product (id:Number, prodName:String, 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. Do 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. Write methods within the class that will access these variables directly. Different methods should 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. Do 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

Void :
public function setProductName(productName:String) :Void
{
  this.productName=productName;
}

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 selecting File > New > ActionScript File (Not Flash Document). Save the document with the name Product.as.

  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. Surround the class keyword with the constructor function. 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. 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);
    }
    
         public function setID (id:Number) :Void
    {
           this.id = id;
    }
    
         public function setProdName (prodName:String) :Void
    {
           this.prodName = prodName;
    }
         public function setDescription (description:String) :Void
    {
           this.description = description;
    }
         public function getID () :Number {
           return id;
    }
         public function getProdName () :String {
           return prodName
    }
    
         public function getDescription () :String {
           return description;
       }
    }
    
  6. Save your file.