PDA

View Full Version : Picking a random question


aphexburn
02-06-2006, 08:37 PM
I am working with a file that is pulling a question from an XML file and displaying it in flash. That part is working just fine, I want to take this further and have it randomly select a question from the XML file so it doesn't just go down the list of questions one thru ten


function QuizItem(question) {
this.question = question;
this.getQuestion = function() {
return this.question;
};

above is the function I am using

Any help or ideas would be great thanks in advance!

flashead
02-06-2006, 09:18 PM
when you load your questions in from the xml file, store them to an array.
then just use random() to grab a random question from that array.
ie.
var myQuestions = [
"If you throw a cat out a car window does it become kitty litter?",
"If corn oil comes from corn, where does baby oil come from?",
"When you choke a Smurf, what color does it turn?"
];

// to preserve the arrays contents use:
var question = myQuestions[ random( myQuestions.length ) ];

// or to remove the question from the array,
// so that you don't end up getting asked the same thing twice use:
var question = myQuestions.splice( random( myQuestions.length ), 1 );

trace( question );

You didn't say how or where that function is being used so I can't really give you any pointers on how to edit it to do what you want.
k.

sophistikat
02-06-2006, 09:22 PM
http://www.actionscript.org/forums/showthread.php3?t=95525