我正在创建一个符合测验风格的FlashCS4应用程序。问题存储在单独的文本文件中,并通过as3调用到程序中。这一切都很好,但我想知道如何随机化这些数据,但要确保相同的问题不会被拉两次。
例如,在我导航到问题页面的那一刻,我可以显示问题的每个部分(回答a,b,c,d +问题本身),然后这可以进行10次。
我想要做的是从文本文件中的(27?)问题中随机生成这10个问题。
import flash.geom.Transform;
import flash.geom.ColorTransform;
import fl.motion.Color;
var glowFilter:GlowFilter = new GlowFilter(0x000000, 1, 2, 2, 10, 3)
var questionNumber:int = 0;
var totalCorrect:int = 0;
var selectedAnswer:String;
var checkAnswer:String;
var correctAnswer:String;
var questionCount:int = 0;
var numberOfQuestions:int = 10;
txt_loggedin_Question.text = (userName);
//Displays the Question Number which is called in from XML
txt_QuestionNumber.text = ("Question #"+questions[questionNumber].ref +" of"+numberOfQuestions);
function CheckAnswer() {
if (selectedAnswer == correctAnswer){
totalCorrect = totalCorrect + 1;
trace("Correct");
}else{
totalCorrect = totalCorrect;
trace("incorrect");
}
questionNumber = questionNumber + 1;
questionCount = questionCount + 1;
//Random questions set up new variable questioncount
if (questionCount == numberOfQuestions){
trace("we are at 10");
gotoAndStop (1, "Result");
//STOP RUN NEXT SCENE
}else{
setUpQuestions()
}
缺少相当多的代码,但我希望这涵盖了必需品,文件在单独的页面上调用,
var questions:Array = [ ];
var request:URLRequest = new URLRequest("1.txt");
var loader:URLLoader = new URLLoader(request);
loader.addEventListener(Event.COMPLETE, completeHandler);
function completeHandler(event:Event):void
{
// loader data - the questions.txt file
var data:String = event.target.data;
// split data by newline for each question
var lines:Array = data.split("\n");
// for every line
for each (var line:String in lines)
{
// split line by "||" delimiter
var question:Array = line.split("||");
// add the question to the questions array:
questions.push({ref: question[0],
question: question[1],
answerA: question[2],
answerB: question[3],
answerC: question[4],
answerD: question[5],
answerE: question[6],
correct: question[7],
answer: question[8],
type: question[9],
file: question[10]});
}
}
所有这一切都有效,但我唯一要努力的是每次加载场景时从文本文件中随机生成问题。很抱歉这个冗长的问题。
感谢阅读。
答案 0 :(得分:0)
以下是您需要执行的步骤:
将您的问题列表作为名为“orderedQuestions”的数组获取。
创建第二个名为“shuffledQuestions”的空数组。
创建while循环:
while(orderedQuestions.length > 0){
shuffledQuestions.push(orderedQuestions.splice(Math.floor(Math.random()*orderedQuestions.length),1));
}
循环从排序列表中随机删除一个问题并将其添加到随机列表中。完成后,您的排序列表将为空,并且shuffledQuestions将以随机顺序添加所有问题。