Learn Actionscript - AHFX

Basic Actionscript - Using Your Custom Class in Your Actionscript 

ahfx actionscript learning actionscript

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

Actionscript 2.0 Flash Implementation

Now we are going to take our new Actionscript 2.0 class and use it in a Flash application. Make sure that you have saved the final version of your actionscript and close down Flash. Open it back up and this time start a new Flash Document. Create a keyframe and enter the following code:

import com.ahfx.Product;
var myProduct = new Product();
myProduct.setQuantity(3);
myProduct.setPrice(2.99);
myProduct.setDesc("Teddy Bear");

trace(myProduct.getDesc());
trace(myProduct.getPrice());
trace(myProduct.getQuantity());
trace(myProduct.getTotal());
 

This will output the following:

Teddy Bear
2.99
3
8.97
 

The first line tells the compiler to import the Product class we had just created. The next line creates an instance of the Product class. Now we start assigning values into the product. This product is a Teddy Bear that costs 2.99, and the person wants 3 of them. The trace statements just print out the values of this product. That was easy wasn´t it. Let´s make another product.

import com.ahfx.Product;
var myProduct = new Product();
myProduct.setQuantity(3);
myProduct.setPrice(2.99);
myProduct.setDesc("Teddy Bear");

trace(myProduct.getDesc());
trace(myProduct.getPrice());
trace(myProduct.getQuantity());
trace(myProduct.getTotal());

var myOtherProduct = new Product();
myOtherProduct.setQuantity(1);
myOtherProduct.setPrice(12.99);
myOtherProduct.setDesc("Cool Sunglasses");
trace(myOtherProduct.getDesc());
trace(myOtherProduct.getTotal());
myOtherProduct.setQuantity(2);
trace(myOtherProduct.getTotal());
 

Notice that we don´t need another import statement. All we do is create another instance of our Actionscript 2.0 class. This instance is named myOtherProduct. It is a pair of cool sunglasses that cost 12.99. The customer decides to get an extra pair (because they are so cool) so we call the setQuantity method and pass it the parameter of 2. You can see that the total automatically updates itself in the new output:

Teddy Bear
2.99
3
8.97
Cool Sunglasses
12.99
25.98
 

This ends the basic Actionscript 2.0 lesson. Although this has been a simple walkthrough, this is a solid base that we wish to build upon. If you continue to the intermediate page, you will learn how to take the product you have created and place it in a shopping cart class.
 
Continue to intermediate page.
 
We would love to hear your feedback. Please email us as info AT ahfx DOT com.


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