
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:
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.
|
 |

Featured Tutorials
|