Create your own data structure and use it.
I’ve made a TriviaObject
that contains its question and answers that has some helper functionality.
public class TriviaObject extends Object
{
protected var _question:String;
protected var _correctAnswer:String;
protected var _answers:Vector.<String>;
/** The trivia question. */
public function get question():String { return _question; }
/** The list of potential answers to the trivia question. */
public function get answers():Vector.<String> { return _answers; }
/**
* Constructor
* @param question The trivia question.
* @param correctAnswer The correct answer for the question.
* @param incorrectAnswers Incorrect answers to confuse the user.
*/
public function TriviaObject(question:String, correctAnswer:String, ... incorrectAnswers)
{
// Save the question and correct answer.
_question = question;
_correctAnswer = correctAnswer;
// Add the correct answer from the list of potential answers.
_answers = new Vector.<String>();
_answers.push(correctAnswer);
// Add all valid incorrect answers to the list of potential answers.
for each (var s:String in incorrectAnswers)
{
_answers.push(s);
}
}
/**
* Checks if the provided guess is the correct answer.
* @param guess The guess at the answer of the question.
* @return Is the guess correct?
*/
public function checkAnswer(guess:String):Boolean
{
// Prevent issues with case sensitivity by converting all answers to lower case.
return guess.toLowerCase() == _correctAnswer.toLowerCase();
}
}
From here, you can create your questions and set up a system to shuffle them, as well as the answers.
I’ve created some sample logic flow.
// Create a list of trivia objects.
var triviaList:Vector.<TriviaObject> = new Vector.<TriviaObject>();
var currentQuestion:TriviaObject;
var currentAnswers:Vector.<String>;
var currentIndex:uint = 0;
var score:uint = 0;
// Populate that list with choices.
triviaList.push(new TriviaObject(
"What year was HMS Active launched?",
"1929",
"1931",
"1943",
"1991"));
triviaList.push(new TriviaObject(
"The Murchison Highway (A10) runs from the West Coast of Tasmania to where?",
"Burnie",
"Smithton",
"Perth",
"Sorell"));
triviaList.push(new TriviaObject(
"What topics the German internet radio show \"RadioTux\" discuss?",
"Free and open source software",
"Bespoke formalware",
"Germanic history",
"Local charity events around Cologne"));
// Randomize list
FP.shuffle(triviaList);
// Begin questions.
currentQuestion = triviaList[currentIndex];
currentAnswers = currentQuestion.answers;
FP.shuffle(answers);
// Display answers and await user selection.
...
// Handle user selection. I added a scoring system for fun.
score += currentQuestion.checkAnswer(userAnswer) ? 100 : -50;
// Proceed to the next question
currentIndex++;