Pulling questions from a xml randomly

5 posts

Flag Post

Hey,

I am working on a game which also contains a quiz. For the quiz the questions and answers are pulled from a xml. The problem is the it always loads the first question in the xml first and i would like this to be more random.

var questions:Array=new Array();
var answers:Array=new Array();

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, loadXML);
loader.load(new URLRequest("sample.xml"));

trace(score);

function loadXML(e:Event):void
{
        var myxml = new XML(e.target.data);
		var loop = score;
		for (var i= 0 ;i<(loop);i++){questions[i]=myxml.ques[i].q1;
		answers[i]=[myxml.ques[i].op1,myxml.ques[i].op2,myxml.ques[i].op3];
 
		}
		gotoAndPlay(currentFrame + 1);
		}
stop();

This is the code that loads the xml

I tried this but it didn’t work I used 5 but this could also be math.random and (loop + i)

 var myxml = new XML(e.target.data);
		var loop = score;
		for (var i= 5 ;i<(loop + 5);i++){questions[i]=myxml.ques[i].q1;
		answers[i]=[myxml.ques[i].op1,myxml.ques[i].op2,myxml.ques[i].op3];

Anyone got any idea
this is the tutorial that was used http://flashbynight.com/tutes/quiz/

Thanks

 
Flag Post

I don’t understand the logic of that loop.

Read this to see how to randomize vectors. Look at the second post by Bob.

 
Flag Post

Sorry but that doesn’t really help me

 
Flag Post

if all of your questions are in questions…

questions = [];
answers = [];
var r:int = Math.random()*questions.length;
var q = questions[r]
var a = answers[r]
questions[r]=questions[questions.length-1]
questions.length--
answers[r]=answers[answers.length-1]
answers.length--
 
Flag Post

You said “i would like this to be more random”, where “this” is an array. I gave you a way to randomize arrays.