Learn Actionscript - AHFX

Basic Actionscript - Creating Class Methods 

ahfx actionscript learning actionscript

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

Actionscript 2.0 Methods

I´ve added the other getters and setters. (You use getters and setters to control the values that may be placed into that variable. For example, you couldn't have a negative cost of a product. So you could create some code that checks to see if the value passed to the set method it negative. If it was negative, don't assign the number. You could also check for not number values being passed.

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;
  }

  function getQuantity():Number {
    return quantity;
  }

  function setQuantity(quantity:Number):Void {
    this.quantity = quantity;
  }

  function getDesc():String {
    return desc;
  }

  function setDesc(myVar:String):Void {
    desc = myVar;
  }

}
 

Now we will add one other method, getTotal. This method will return a number that will tell us the total cost of this product (price times quantity)

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;
  }

  function getQuantity():Number {
    return quantity;
  }

  function setQuantity(quantity:Number):Void {
    this.quantity = quantity;
  }

  function getDesc():String {
    return desc;
  }

  function setDesc(myVar:String):Void {
    desc = myVar;
  }
 
  function getTotal():Number {
    return price*quantity;
  }

}
 

This method takes no parameters and returns a Number. You see that it will return price*quantity. Next we will examine the Flash to use this class.

Continue to page 4
 

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