This is a modification of a similar script I wrote a couple of days ago for someone who wanted to load a list of animals.
Put the following into a file called questions.xml, and put the file
the same place the .swf is.
Then put this actionscript on frame 1 of your movie. It loads the questions into an array of question/answer pairs.Code:<questionList> <question q="What is furry and purrs?" a="A cat" /> <question q="What is furry and barks?" a="A dog" /> <question q="What is not furry" a="A shaved cat" /> </questionList>
- jimCode:// XML Question Loader - Jim Bumgardner qList = new Array(); // This is a recursive function that // walks thru all the nodes in your XML // and loads up the ones you're // interested in into the qList array. loadNode = function(it) { if (it.nodeName == 'question') { var qPair = new Array(); qPair.q = it.attributes['q']; qPair.a = it.attributes['a']; qList.push(qPair); } else if (it.nodeName == "questionList") { // process any questionList properties here // - there aren't any at the moment. // Note that the 'questionList' tag is // completely ignored - this will still // work without that tag. } if (it.hasChildNodes()) { for (var i = 0; i < it.childNodes.length; ++i) loadNode(it.childNodes[i]); } } // when this function is called, we're done loading // - do something with your data. // Most likely you want to jump to a frame in your movie where you play the game, but I'll just output a few values function finishInit() { trace(qList.length + " questions loaded:"); // Pick a random question var r = random(qList.length); trace("a random question: " + qList[r].q); trace("...and its answer: " + qList[r].a); } var myxml = new XML(); myxml.onLoad = function() { // initialize array again, in case // we're calling this twice... qList = new Array(); loadNode(this); finishInit(); } myxml.load("questions.xml"); // A common mistake is to attempt to access the data here. Don't -- it is not loaded at this point // myxml.load() is an asynchronous function - it returns before it is finished. // The finishInit() function (above) will be called when the data is ready to go...




Reply With Quote