Hi there,
I’m trying to write a trivia game, which reads in an XML file containing questions and answers. The XML file is formatted like this:
<?xml version="1.0" encoding="utr-8">
<QAnode>
<question>What is Google Chrome?</question>
<answerA>A new kind of metal</answerA>
<answerB>An email client</answerB>
<answerC>A web browser</answerC>
<answerD>A search engine</answerD>
<CorrAns>C</CorrAns>
</QAnode>
...(more QAnodes follow)
I’ve written a class file (TriviaGame.as) which handles the gameplay. I’d like to be able to process the XML file from within the class, via a call to initXML():
private function initXML (filename:String) {
// Create new XML Object and set ignoreWhite true
QA_xml = new XML ();
QA_xml.ignoreWhite = true;
// Setup load handler which just invokes another function
// which will do the parsing of our XML
var thisObj:TriviaGame = this; //Pointer to this object
QA_xml.onLoad = function (sucess) {
if (sucess) {
trace ('loaded file successfully');
thisObj.processQA(QA_xml);
trace('completed processQA');
} else {
trace ('File was not loaded');
}
};
// Load up the XML file into Flash
QA_xml.load (filename);
}
private function processQA (xmlDoc_xml) {
//Initialize QA_array
trace ('Inside processQA');
QA_array = new Array ();
trace('
FirstChild:
' + xmlDoc_xml.firstChild);
trace('
ChildNodes:
' + xmlDoc_xml.childNodes);
trace('
fc.fc:
' + xmlDoc_xml.firstChild.firstChild);
for (var n = 0; n < xmlDoc_xml.firstChild.childNodes.length; n++) {
QA_array[n] = new Array ();
for (var i = 0; i < xmlDoc_xml.firstChild.childNodes[n].childNodes.length; i++) {
//place nodes values into the object
trace('
Node value: ');
trace(xmlDoc_xml.firstChild.childNodes[n].childNodes*.firstChild.nodeValue);
QA_array[n]* = xmlDoc_xml.firstChild.childNodes[n].childNodes*.firstChild.nodeValue;
}
}
trace('Question:' + QA_array[0][0]);
trace('Answers:
A: ' + QA_array[0][1] + '
B: ' + QA_array[0][2] + '
C: ' + QA_array[0][3] + '
D: ' + QA_array[0][4]);
trace('
Correct answer: ' + QA_array[0][5]);
}
The traces are all for debug purposes. If I place this code into the actions panel of the first frame of the flash document (without the thisObj:TriviaGame = this part), everything works fine and I get correct traces.
If I place this code within TriviaGame.as, all of the traces print ‘undefined’, but it claims to have opened the file correctly. I’ve found a lot of XML tutorials on this site, but none of them seem to deal with processing XML from within a class file–they all put the code directly on the frame.
Does anyone know why it would goof up on me like this?
Thanks in advance.