Pulling questions from a xml randomly

5 posts

Flag Post


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"));


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;
		gotoAndPlay(currentFrame + 1);

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;

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


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]
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.