Hi all,
This question may get pretty long winded but if you could help me, it’d be much appreciated because I’m really struggling with this piece of actionscript! :{
Basically I’m trying to create a quiz, and I thought the best way to do this would be to create a form based flash ap, which loads the questions and answers from an xml file.
Once I’ve loaded the xml file I want to put it’s contents into an array, so I can extract questions/answers/alternate answers etc from it with greater ease than with xml.
It’s at this point I’m really struggling. I get no compile time errors and when I run the file it seems to put all the values into their arrays and these arrays into other arrays. But once I’m outside of this storing loop all of the values have gone. Now I’m pretty sure this isn’t down to a typo (If it is, I swear I’ll go crazy!)
I figure it’s best to give some code… so here goes:
the XML I’m using looks something like this:
<scenario title="Scen A" name="Tom">
<question><p>How many roads does..</p></question>
<answer correctAns="2"><p>The correct answer is..</p></answer>
<altAnswer altselection="3"><p>I dunno</p></altAnswer>
</scenario><scenario title="Scen B" name="Bob">
<question><p>Meaning of Lifep></question>
<answer correctAns="2"><p>42 isn't it?</p></answer>
<altAnswer altselection="3"><p>Maybe it was 43?</p></altAnswer>
</scenario>
and the actual actionscript which is trying to rip all that apart is this:
var answers:XML = new XML();
answers.ignoreWhite=true;
answers.load(xml);
answers.onLoad = function(sucess:Boolean):Void {
if (sucess == true) {
var scenarios:Array = new Array();
for (var i:Number=0; i<this.childNodes.length; i++) {
var secondLvlData:Array = new Array();
//This for loop will pull attributes from top level nodes (Scenario nodes)
for (attr in this.childNodes*.attributes) {
secondLvlData[attr] = this.childNodes*.attributes[attr];
}
//This for lopp will pull data from 2nd level nodes (question, Answer, Alt)
for (var j:Number=0; j<this.childNodes*.childNodes.length; j++) {
var thirdLvlData:Array = new Array();
for (attr in this.childNodes*.childNodes[j].attributes) {
thirdLvlData[attr] = this.childNodes*.childNodes[j].attributes[attr];
}
thirdLvlData[this.childNodes*.childNodes[j].nodeName] = this.childNodes*.childNodes[j].firstChild;
}
//none of these traces work!
trace(secondLvlData)
secondLvlData["data"] = thirdLvlData;
trace(secondLvlData["Data"]);
scenarios.push(secondLvlData);
}
} else {
trace("Loading of xml file failed");
}
};
I’m really sorry it’s such a beast of a question, but if anyone can give me any pointers it’d be greatly appreciated! :thumb2:
Cheers,
daf