Arrays 如何在actionscript3中随机化测验问题?
我有一个flash的问答游戏;基本上你需要输入答案来回答4个问题。最后,它会显示分数和你的答案与正确答案的对比Arrays 如何在actionscript3中随机化测验问题?,arrays,actionscript-3,random,Arrays,Actionscript 3,Random,我有一个flash的问答游戏;基本上你需要输入答案来回答4个问题。最后,它会显示分数和你的答案与正确答案的对比 我需要关于如何随机化问题的帮助(不要重复问题) 最后,正确答案需要与玩家回答问题的顺序相匹配 我把照片附在下面 代码:第1帧 stop(); var nQNumber:Number = 0; var aQuestions:Array = new Array(); var aCorrectAnswers:Array = new Array("Jupiter", "Mars", "war
stop();
var nQNumber:Number = 0;
var aQuestions:Array = new Array();
var aCorrectAnswers:Array = new Array("Jupiter", "Mars", "war", "Titan");
var aUserAnswers:Array = new Array();
aQuestions[0] = "What is the biggest planet in our solar system?";
aQuestions[1] = "Which planet in our solar system is the 4th planet from the
sun?";
aQuestions[2] = "Mars is named after the Roman god of ___.";
aQuestions[3] = "What is the name of Saturn's largest moon?";
questions_txt.text = aQuestions[nQNumber];
submit_btn.addEventListener(MouseEvent.CLICK, quiz);
function quiz(e:MouseEvent):void{
aUserAnswers.push(answers_txt.text);
answers_txt.text = "";
nQNumber++;
if(nQNumber < aQuestions.length){
questions_txt.text = aQuestions[nQNumber]}
else{
nextFrame()}
}
stop();
变量nQNumber:Number=0;
var aQuestions:Array=new Array();
var aCorrectAnswers:数组=新数组(“木星”、“火星”、“战争”、“泰坦”);
var aUserAnswers:Array=new Array();
问[0]=“我们太阳系中最大的行星是什么?”;
问[1]=“我们太阳系中的哪颗行星是地球上第四颗行星?”
太阳;
aQuestions[2]=“火星是以罗马的___;神命名的。”;
问[3]=“土星最大的卫星叫什么名字?”;
questions_txt.text=aQuestions[nQNumber];
提交\u btn.addEventListener(MouseEvent.CLICK,测验);
功能测验(e:MouseeEvent):无效{
push(answers_txt.text);
答案_txt.text=“”;
nQNumber++;
if(nQNumber
第2帧
var nScore:Number = 0;
for(var i:Number = 0; i < aQuestions.length; i++){
this["userAnswer" + i + "_txt"].text = aUserAnswers[i];
this["correctAnswer" + i + "_txt"].text = aCorrectAnswers[i];
if(aUserAnswers[i].toUpperCase() == aCorrectAnswers[i].toUpperCase()){
nScore++}
if(i == aQuestions.length - 1){
score_txt.text = nScore.toString()}}
var nScore:Number=0;
对于(变量i:Number=0;i
您正在寻找名为的漂亮功能吗?将Math.random()的结果乘以所需的随机生成范围,将其四舍五入,然后使用数字在数组中选择一个随机问题。是否正在查找名为的漂亮功能?将Math.random()的结果乘以所需的随机生成范围,四舍五入,然后使用数字在数组中选择一个随机问题。处理此问题的常用方法是创建一个数组来容纳需要提问的问题,将数组随机化,然后在询问时从数组中删除相应的问题。然后,当该数组为空时,转到重述屏幕
以下是实现此目标的多种方法之一:
首先,让我们通过使用对象而不是一大堆数组来简化这个过程。您的对象将具有所有相关信息的属性
//create an array that will hold all your questions
var questions:Array = [];
//add a new question object to the array, repeat for all questions
questions.push({
question: "What is the biggest planet in our solar system?",
correctAnswer: "Jupiter"
userAnswer: null,
correct: false
});
接下来,让我们将该数组随机化:
//sort the array with the sort function below
questions.sort(randomizeArray);
//this sorts in a random way
function randomizeArray(a,b):int {
return(Math.random() > 0.5) ? 1: -1;
}
现在,让我们复制数组以跟踪哪些问题仍然需要提问
var askQuestions:Array = questions.concat(); //concat with no parameters returns a new shallow copy of the array
var curQuestion; //create a var to hold the current question
现在,创建一个函数来询问下一个问题:
function askNextQuestion():void {
//check if there are any more questions to ask
if(askQuestions.length > 0){
//get the next question object
curQuestion = askQuestions.shift(); //shift removes the first item of an array, and returns that item
questions_txt.text = curQuestion.question;
answers_txt.text = "";
}else{
//all questions have been asked, show your recap screen
finish();
}
}
单击“应答”按钮时,需要运行一个函数:
function submitAnswer(e:Event = null):void {
//if there is a current question
if(curQuestion){
curQuestion.userAnswer = answers_txt.text;
curQuestion.correct = curQuestion.correctAnswer.toUpperCase() == answers_txt.text.toUpperCase();
}
//ask the next question
askNextQuestion();
}
以及一个在询问所有问题后运行的函数:
function finish():void {
var score:int = 0;
//go through the array and count how many are correct and recap
for(var i:int=0; i<questions.length;i++){
if(questions[i].correct) score++;
trace("Question " + (i+1) + ":",questions[i].question); //arrays are 0 based, so we add 1 to the index (i) so that it says "Question 1:" for the first question instead of "Question 0:"
trace("You answered:",questions[i].userAnswer);
trace("Correct Answer:", questions[i].correctAnswer);
trace(questions[i].correct ? "You were correct" : "You were wrong","\n"); //this is shorthand if statement, \n is a line break
}
score_txt.text = score + " out of " + questions.length;
}
函数finish():void{
变量得分:int=0;
//检查数组,数一数有多少是正确的,然后重述一遍
对于(var i:int=0;i处理此问题的常用方法是创建一个数组来容纳需要提问的问题,将数组随机化,然后在提问时从数组中删除相应的问题。然后,当该数组为空时,转到重述屏幕
以下是实现此目标的多种方法之一:
首先,让我们通过使用对象而不是一大堆数组来简化这个过程。您的对象将具有所有相关信息的属性
//create an array that will hold all your questions
var questions:Array = [];
//add a new question object to the array, repeat for all questions
questions.push({
question: "What is the biggest planet in our solar system?",
correctAnswer: "Jupiter"
userAnswer: null,
correct: false
});
接下来,让我们将该数组随机化:
//sort the array with the sort function below
questions.sort(randomizeArray);
//this sorts in a random way
function randomizeArray(a,b):int {
return(Math.random() > 0.5) ? 1: -1;
}
现在,让我们复制数组以跟踪哪些问题仍然需要提问
var askQuestions:Array = questions.concat(); //concat with no parameters returns a new shallow copy of the array
var curQuestion; //create a var to hold the current question
现在,创建一个函数来询问下一个问题:
function askNextQuestion():void {
//check if there are any more questions to ask
if(askQuestions.length > 0){
//get the next question object
curQuestion = askQuestions.shift(); //shift removes the first item of an array, and returns that item
questions_txt.text = curQuestion.question;
answers_txt.text = "";
}else{
//all questions have been asked, show your recap screen
finish();
}
}
单击“应答”按钮时,需要运行一个函数:
function submitAnswer(e:Event = null):void {
//if there is a current question
if(curQuestion){
curQuestion.userAnswer = answers_txt.text;
curQuestion.correct = curQuestion.correctAnswer.toUpperCase() == answers_txt.text.toUpperCase();
}
//ask the next question
askNextQuestion();
}
以及一个在询问所有问题后运行的函数:
function finish():void {
var score:int = 0;
//go through the array and count how many are correct and recap
for(var i:int=0; i<questions.length;i++){
if(questions[i].correct) score++;
trace("Question " + (i+1) + ":",questions[i].question); //arrays are 0 based, so we add 1 to the index (i) so that it says "Question 1:" for the first question instead of "Question 0:"
trace("You answered:",questions[i].userAnswer);
trace("Correct Answer:", questions[i].correctAnswer);
trace(questions[i].correct ? "You were correct" : "You were wrong","\n"); //this is shorthand if statement, \n is a line break
}
score_txt.text = score + " out of " + questions.length;
}
函数finish():void{
变量得分:int=0;
//检查数组,数一数有多少是正确的,然后重述一遍
对于(var i:int=0;i当我尝试此代码时,我得到以下错误类型error:error#1009:无法访问空对象引用的属性或方法。在Untitled#u Scene1#u fla::main timeline/askNextQuestion()在Untitled#u Scene1#fla::main timeline/frame1()什么是“q”假设是在问题中\u txt.text=q.question;而在q.correct in finish函数上?q是第一位的输入错误(应该是curQuestion
,而不是q
)。在我答案末尾的for each循环中,q代表循环当前迭代的问题对象(虽然我看到我忘了把var
放在它前面了。)我更新了答案,修正了两个打字错误/错误。关于你问题中的新信息(自从我回答后)。问题
数组的顺序将与询问的顺序相同。(将问题更改为问题后。弹出到问题。shift
)所以,当测验结束时,你可以重复每个问题来重述-我用traces做了一个例子(在AnimateCC/FlashProthanks中测试/运行它时在输出窗口中显示),它现在以我想要的方式工作,但我想知道为什么在trace上总是说“你是正确的”,即使答案是错误的“curQuestion.correct=curQuestion.userAnswer.toUpperCase()==answers_txt.text.toUpperCase();”在submitAnswer函数中,当我这样做时,它每次都会说“你错了”。我调整了finish函数,使它在动态文本框中显示真实分数,但在trace中,它仍然会说“你错了”,因为我已经注释了”curQuestion.correct=curQuestion.userAnswer.toUpperCase()==answers_txt.text.toUpperCase();”。只是一个输入错误,您应该将正确答案与文本框进行比较。正如我所写的,您正在将用户的答案与用户的答案进行比较(这总是正确的)我会更新答案并修正它。如果你觉得这个答案回答你的问题,考虑把它标记为“绿色检查”,以帮助未来的访问者快速地看到解决方案。它也会增加你(和我)的声誉。