I have three dynamic text boxes. variables respectively
(caption0,caption1,caption2).
My xml file is structured as:
<?xml version="1.0" encoding="iso-8859-1"?>
<captions>
<caption>Roll over this</caption>
<caption>This is the first Caption.</caption>
<caption>This is the second Caption.</caption>
</captions>
I am trying to create a rollover where when you rollover caption0; caption1
and caption2 are displayed in sequence.
Everything works except though only the first caption is ever displayed. For
the other two I get "level0.caption1" and "level0.caption2" instead of the text
in the xml file above.
Help!
This has got to be simple and I am missing something.
How do I parse the data so that each of the variables are assigned the text in
the xml file?
MotionMaker - 31 Jan 2007 14:55 GMT
Here is the Actionscript parsing for the XML you posted.
Create a new movie and save in folder with xml. Place a ComboBox component on
stage and give it an instance name of captions_cb. Add the code below to frame
1 and change the xml file name to one you have.
/*------------------------------------------------------------------------
The
------------------------------------------------------------------------*/
var captions_xml:XML = new XML()
captions_xml.ignoreWhite = true
/*------------------------------------------------------------------------
Even handler for the captions_xml onLoad event
------------------------------------------------------------------------*/
captions_xml.onLoad = function(success:Boolean)
{
trace (success)
trace (this.status)
if ( success && this.status == 0 )
{
/*------------------------------------------------------------------------
Iterate each captions->caption node in xml
------------------------------------------------------------------------*/
for (var aNode:XMLNode = this.firstChild.firstChild; aNode != null; aNode =
aNode.nextSibling)
{
/*------------------------------------------------------------------------
Add item to captions_cb from caption node
------------------------------------------------------------------------*/
captions_cb.addItem( { label:aNode.firstChild.nodeValue, data:undefined } );
}
/*------------------------------------------------------------------------
Set the selected index for captions_cb
------------------------------------------------------------------------*/
captions_cb.selectedIndex = 0;
captions_cb_Changed( { target:captions_cb } )
}
}
/*------------------------------------------------------------------------
Load the XML data.
------------------------------------------------------------------------*/
captions_xml.load("XMLCaptions.xml");
/*------------------------------------------------------------------------
The change event handler for captions_cb
------------------------------------------------------------------------*/
function captions_cb_Changed(evt:Object)
{
url_tf.text = evt.target.selectedItem.data;
}
/*------------------------------------------------------------------------
Register the change event handler for captions_cb
------------------------------------------------------------------------*/
captions_cb.addEventListener( "change", captions_cb_Changed );
/*------------------------------------------------------------------------
The click event handler for go_button
------------------------------------------------------------------------*/
function go_button_Click(evt:Object)
{
getURL( captions_cb.selectedItem.data, "_blank" );
}
/*------------------------------------------------------------------------
Register the click event handler for wgo_button
------------------------------------------------------------------------*/
go_button.addEventListener("click", go_button_Click);
sirrahe - 31 Jan 2007 16:51 GMT
MotionMaker
I appreciate the quick response. I followed the instructions and the xml
captions were added to the combo box.
But I still do not have an answer for my issue, which is parsing the xml data
and assigning to the variables.
I chose the variable names such that I could do a for loop incrementing "i"
and appending the value of "i" to the end of the variable.
Here is the script below:
var rollover:XML = new XML();
rollover.ignoreWhite = true
rollover.onLoad = function(success) {
if (success){
for (var aNode:XMLNode = this.firstChild.firstChild; aNode != null; aNode =
aNode.nextSibling)
{
i=0;
trace(this.ref["caption"+i] = aNode.firstChild.nodeValue);
i++;
}}
else
trace("Error loading XML document");
}
rollover.load("rollover.xml")
stop();
The output of the trace is:
"Roll over this"
"This is the first Caption."
"This is the second Caption."
Yet the actual text boxes display:
"_level0.caption1"
"_level0.caption2"
"_level0.caption3"
MotionMaker - 31 Jan 2007 18:22 GMT
Perhaps
this.ref["caption"+i] = aNode.firstChild.nodeValue;
should be
this.ref["caption"+i].text = aNode.firstChild.nodeValue;
Also tracing an assignment does not seem useful:
trace(this.ref["caption"+i] = aNode.firstChild.nodeValue);
This would however
trace(aNode.firstChild.nodeValue)
sirrahe - 31 Jan 2007 19:07 GMT
I first tried:
this.ref["caption"+i].text = aNode.firstChild.nodeValue;
It did not make a difference. As far as the trace, it at least proved to me
that I was assigning the correct data from the xml file.
I guess the statement above is not changing the actual variables (caption0,
caption1, and caption2).
Is there a way to display the values of the variables in using trace or
display?
MotionMaker - 31 Jan 2007 19:51 GMT
The problem maybe the this.ref variable. In this case this refers to
rollover:XML.
Perhaps it should read
_root["caption"+i].text = aNode.firstChild.nodeValue;
To display a variable in Flash you use trace(exp)
Ex: trace(this.ref["caption"+i]) would give you undefined, the instance name
or a value depending on what it is.
sirrahe - 31 Jan 2007 20:23 GMT
First let me thank you for your patience. We are making progress.
Replacing "this.ref" with "_root" did correct the first variable. It now
displays the xml data correctly in the text box.
The text boxes for the second and third variable still display
"_level0.caption2" and "_level0.caption3" respectively.
Could it be that the 2nd and third textboxes are not in frame 1? They are not
displayed until a button is rolled over and the movie is advanced to their
frames (5&10).
MotionMaker - 31 Jan 2007 20:59 GMT
General rule of thumb is to have the authoring time objects instantiated (on
frames) before the code uses them and must exist on any frame having code that
will use them.
It probably a good idea is to have on frame 2 the XML loading code that
references authoring time objects and the authoring time objects on frame 1.
This is particularly true of MovieClips. I am leaning on that it is likely for
the TextFields but not fully certain.
You can set the TextField _visible property to false and when you need to show
them set the _visible property true.
sirrahe - 30 Mar 2007 14:31 GMT
Thank you so much.
Problem solved.