I’ve been following a tutorial that gave me the following two functions for a randomized question from an XML list:
var questions:Array=new Array();
var answers:Array=new Array();
var totalAmountWon:Number = 0;
var amountWonThisRound:Number;
var loader:URLLoader = new URLLoader();
loader.load(new URLRequest("sample.xml"));
loader.addEventListener(Event.COMPLETE, loadXML);
function loadXML(e:Event):void
{
var myxml = new XML(e.target.data);
var loop =myxml.ques.length();
for (var i=0;i<loop;i++){questions*=myxml.ques*.q1;
answers*=[myxml.ques*.op1,myxml.ques*.op2,myxml.ques*.op3];
}
gotoAndPlay(2);
}
function change_question()
{
if (questionnumber == numberOfQuestions)
{
gotoAndPlay(5);
}
else
{
rnd1 = Math.ceil(Math.random() * 3);
rnd2 = Math.ceil(Math.random() * questions.length) - 1;
q.text = questions[rnd2];
if (questions[rnd2] == "x")
{
change_question();
}
questions[rnd2] = "x";
if (rnd1 == 1)
{
shade1.opt1.text = answers[rnd2][0];
shade2.opt2.text = answers[rnd2][1];
shade3.opt3.text = answers[rnd2][2];
}
if (rnd1 == 2)
{
shade1.opt1.text = answers[rnd2][2];
shade2.opt2.text = answers[rnd2][0];
shade3.opt3.text = answers[rnd2][1];
}
if (rnd1 == 3)
{
shade1.opt1.text = answers[rnd2][1];
shade2.opt2.text = answers[rnd2][2];
shade3.opt3.text = answers[rnd2][0];
}
}
}
Eventually, I started hitting a stack overflow around the q.text line, and even though I’m a total n00b, I noticed that the function is calling itself, which I’m told is bad code. Because I’m a total n00b, however, I don’t know how to correct it. Any advice?