我正在创建一个测验风格的 FlashCS4 应用程序。这些问题存储在一个单独的文本文件中,并通过 as3 调用到程序中。这一切都很好,但是我想知道如何随机化这些数据,但要确保同一个问题不会被拉两次。
例如,当我导航到问题页面时,我可以显示问题的每个部分(答案 a、b、c、d + 问题本身),然后可以进行 10 次。
我要做的是让这 10 个问题从我在文本文件中的(27 个?)问题中随机生成。
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]});
}
}
所有这些都有效,但我唯一苦苦挣扎的是每次加载场景时从文本文件中随机生成问题。很抱歉这个冗长的问题。
谢谢阅读。