User Guide
Display gift details 37
Add code to the ProductForm movie clip
Next, you will add ActionScript to the ProductForm movie clip that you just created. The
ActionScript populates the components in the movie clip with information about the selected
gift, and adds an event listener to the Add to Cart button that adds the selected product to
the cart.
For more information on working with event listeners, see “Using event listeners” in Using
ActionScript in Flash.
1. In the Timeline of the ProductForm movie clip, create a new layer and name it Actions.
Select the first frame in the Actions layer.
2. In the Actions panel, add the following code:
// Create an object to reference the selected product item in the
DataGrid.
var thisProduct:Object = this._parent._parent.products_dg.selectedItem;
// Populate the description_ta TextArea and price_lbl Label instances
with
// data from the selected product.
description_ta.text = thisProduct.description;
price_lbl.text = "<b>$"+thisProduct.price+"
"+thisProduct.priceQualifier+"</b>";
// Load an image of the product from the application directory.
image_ldr.load(thisProduct.image);
First, the code defines a variable to refer to the selected product in the subsequent code.
Using the
thisProduct variable means you don’t have to refer to the specified product
using the path
this._parent._parent.products_dg.selectedItem.
Next, the code populates the TextArea and Label instances by using the
description,
price, and priceQualifier properties of the thisProduct object. These properties
correspond to elements in the products.xml file that you linked to the
products_xmlcon
XMLConnector instance at the beginning of the tutorial. Later in the tutorial, you will
bind the XMLConnector, DataSet, and DataGrid component instances together, and the
elements in the XML file will populate the other two component instances.
Finally, the code uses the
image property of the thisProduct object instance to load an
image of the product into the Loader component.
NOTE
The code includes comments explaining its purpose. It’s a good idea to include
comments like these in all the ActionScript code you write, so that you or anyone
else going back to the code later can easily understand what it was for.