For the past couple of days I’ve been working on developing a creative way to show Progress summaries for all the chapters in the courseware I have been developing. Instead of having a long list of Chapter and Topic titles listed and their respective progress status displayed, I decided to use the Accordion component to organize and display my individual Progress summaries. In the words of Macromedia the Accordion component is a navigator that contains a sequence of children that it displays one at a time.
However I needed the children for my Accordion to be dynamically created based on information from a XML file.
Solution:
Open a new .fla document and save it as accordion.fla. From the Components panel drag the Accordion component on to your timeline and assign it the instance name myAccordion.
Now create your XML file in Dreamweaver. For this sample, use the following code and save your file as accordion.xml in the same directory as your newly created fla file.
<node>
<node label=”Label One” />
<node label=”Label Two” />
<node label=”Label Three” />
<node label=”Label Four” />
<node label=”Label Five” />
<node label=”Label Six” />
</node>
Now back to Flash. Create a new layer and call it Actions. Select the first key frame in this layer and insert the following actionscript.
myXML = new XML();
myXML.ignoreWhite = true;
myXML.load(”accordion.xml”);
We have just created a new XML object and loaded the XML-formatted data (accordion.xml) from an external source. myXML.ignoreWhite is set to true so that all white space in the XML document is ignored when the data is parsed.
Now insert the following actionscript directly below.
myXML.onLoad = function() {
var i:Number = myXML.firstChild.childNodes.length;
for (var n=1; n
If you don’t know what the above actionscript is all about then continue reading.
What this does is create a child for each node in the XML document on our Accordion component that we assigned the instance myAccordion to.
First we need to determine the number of childNodes in the firstChild of the XML file and assign that number a variable called i. In this example, ‘i’ should return a value of 6.
var i:Number = myXML.firstChild.childNodes.length;
Next we will create a loop.
for (var n=1; n
Notice that the number of iterations that the loop will perform (variable i), is the number of childNodes in the firstChild of the XML file.
We will now have to perform the following actions inside the loop.
var accordionLabel:String = myXML.firstChild.childNodes[n-1].attributes.label;
myAccordion.createChild(”View”, n, {label:accordionLabel});
First we determine the value of the node label in the XML file and assign it a variable called accordionLabel. We’ve used the variable “n” to also change the childNode number and create a different childInstanceName each time the loop is performed. Keep in mind that to reference the first childNode always use zero. That’s why we have “n-1”
After we create the variable “accordionLabel” we can now create the child with an instance name based on the variable “n”, and a label based on the variable “accordionLabel”.
Save and test movie.
Click here to download example files.