Learn Actionscript - AHFX

Basic Actionscript - Class Constructor and Variables 

ahfx actionscript learning actionscript

AHFX Actionscript Home
Basic Actionscript
Intermediate Actionscript
Advanced Actionscript
About AHFX
Contact AHFX
Great Actionscript Links
 
 
 
 

Actionscript 2.0 Constructor

This actionscript class won't do much as of yet. We can't even create an instance of our class yet. We must create a constructor. A constructor is a special method that creates an instance of an Actionscript 2.0 class. A constructor has the same name as the class. The code now reads like this for Product.as.

class com.ahfx.Product {

  function Product() {

  }

}

Now that we have created a constructor. We could create an actual Product in a Flash application from our Actionscript 2.0 class Product.as (We will examine the Flash side in a while).

Actionscript 2.0 Attributes

However, even though we could create an instance, this also wouldn't do much. Lets add some data (the first half of an Actionscript 2.0 class) to our actionscript class. This class will describe Products that will go in a Shopping Cart (Creating the shopping cart is found in the Intermediate lesson). Our basic product with have a description, a price, and a quantity. Although this is a very simple example, you could add more attrinbutes later.

class com.ahfx.Product {

//Instance Variables
  private var price:Number;
  private var desc:String;
  private var quantity:Number;

  function Product() {

  }

}
  • private — Tells the class that only this actionscript class and its subclasses can access this variable.
  • var — Tells the Actionscript 2.0 class that this is a variable.
  • :Datatype — Anything after the : is the datatype of the variable. (For example Number above).

These variables are instance variables. This means that every time we create an instance of a Product, it will have its own desc, price, and quantity.For example, we could create a Product with a desc="Teddy Bear", price=14.99, and quantity=3. We could then create another Product with a desc="Soyo Motherboard", price=68.99, and quantity=1. Each of these Products is an instance. They have their own variables.

Get and Set Actionscript 2.0 Variables

Now that we have the first part of an actionscript class done(the data part), we will begin on the second part(the methods). Because we have declared the variables to be private, we must create get and set methods for the variables. This allows our Flash application to read and write to the variables.

class com.ahfx.Product {

//Instance Variables
  private var price:Number;
  private var desc:String;
  private var quantity:Number;

  function Product() {

  }

  function getPrice():Number {
    return price;
  }

  function setPrice(price:Number):Void {
    this.price = price;
  }

}

Lets examine the getPrice() function first. getPrice() is the name of the function. :Number is the return type. This means that you could write the following code in your Flash application, var myPrice:Number = myProduct.getPrice();, because getPrice() returns a Number. return price; takes the instance variable "price" and returns it to the program that called it.

Next lets examine the setPrice() function in our Actionscript 2.0 class. setPrice() is the name of the function. This function passes (or sends) one variable which is a Number. We will use the name "price" for this local variable. This class doesn't return anything and so the return type is :Void. this.price is a reference to the instance variable price while = price is a reference to the local variable price.

We could have written the code like this also.

class com.ahfx.Product {

//Instance Variables
  private var price:Number;
  private var desc:String;
  private var quantity:Number;

  function Product() {

  }

  function getPrice():Number {
    return price;
  }

  function setPrice(myVar:Number):Void {
    price = myVar;
  }

}

The local variable is myVar this time instead of price. Because we don't have a local variable with the same name as an instance variable we don't need to clarify which price we were talking about and we don't need to add this. in front of price.

Continue to page 3
 

SUPPORT THIS SITE
If this tutorial helped you, show your support and send me something from my wish list.
Click on an item below and then choose
Adam Hayes (Gift Registry Address) for the Ship to Address.
It is that easy!
 

 

Featured Tutorials

Flash Shopping Cart Tutorial
Create a custom Flash Shopping Cart that will add, update, and show products.

Flash MX 2004 Custom Actions
Take your custom classes to the next level and have Flash treat them as built in classes.

Pass Variables to Flash
Quick and dirty way of passing variables to your flash animations.

Affordable Web Design and Hosting
Find out how you can cut your overhead by hosting with AHFX.net

Affordable Custom Art
Get your pictures custom framed. Amazing Prices. Outstanding Quality.


 
AH Digital FX Studios
Disclaimer | Site Map | Idaho Web Design