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");
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");
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:
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.
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!