Now that is a very easy way to drop information from your flash shopping cart into a nice looking table. However, it does come with a mighty overhead (about 40KB). We will now look at making our own output for our shopping cart.
First create an empty movie clip by pressing Ctrl-F8. Name the movie clip empty_mc, and check export for actionscript. Close this clip.
Next create another movie clip and name this one display_mc and also check export for actionscript.On this clip create 4 dynamic text boxes and give them instance names of desc, quantity, price, and total. Also create a button and name it update_btn.
We will create a new display_mc for every item in our cart. Return to the main stage and change the code to look like this:
import com.ahfx.Product;
import com.ahfx.Shopping;
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");
var shop_cart = new Shopping();
shop_cart.addElement(myProduct);
shop_cart.addElement(myOtherProduct);
displayCart();
function displayCart(){ //Create main movie clip to hold shopping cart output
empty_mc = attachMovie("empty_mc","empty_mc2",150); //Create movie clip to hold shopping cart output headings
temp_mc = empty_mc.attachMovie("display_mc","headings_mc",1001);
temp_mc.total.text = "Total";
temp_mc.desc.text = "Description";
temp_mc.quantity.text = "Quantity";
temp_mc.price.text = "Price";
temp_mc.update_btn._visible = false;
delete temp_mc; //Loop through items in shopping cart
for(i=0;i<shop_cart.size();i++){
var newProd:Product = shop_cart.getElement(i); //Attach display for each item in shopping cart
temp_mc = empty_mc.attachMovie("display_mc","display_mc"+i,i+100); //Shift dispay downward 16 pixels
temp_mc._y = (i + 1) * 16; //Place values from product in shopping cart in the text fields
temp_mc.quantity.text=newProd.getQuantity();
temp_mc.desc.text=newProd.getDesc();
temp_mc.price.text=newProd.getPrice();
temp_mc.total.text=newProd.getTotal(); //Position holder of product in shopping cart
temp_mc.pos = i; //button action to update quantities
temp_mc.update_btn.onPress = function() {
var tempProduct:Product = shop_cart.getElement(this._parent.pos);
if (this._parent.quantity.text == "0"){
shop_cart.removeElementAt(this._parent.pos);
}
else {
tempProduct.setQuantity(this._parent.quantity.text);
}
displayCart();
}
delete newProd;
} //Create movie clip to hold shopping cart Grand Total output
temp_mc = empty_mc.attachMovie("display_mc","total_mc",1000);
temp_mc._y = (shop_cart.size() +1) * 16;
temp_mc.total.text = shop_cart.getTotal();
temp_mc.desc.text = "Grand Total";
delete temp_mc;
}
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!