如何在 actionscript3 中随机化测验问题?
How do I randomize quiz questions in actionscript3?
我在 Flash 中有一个问答游戏;基本上你需要输入答案来回答 4 个问题。最后,它会显示分数和您的答案与正确答案。
- 我需要有关如何随机化问题(不重复问题)的帮助
- 最终正确答案需要与玩家回答问题的顺序相匹配
我附上了下面的图片。
代码:第 1 帧
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()}
}
第 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()}}
您是否正在寻找名为 Math.random() 的绝妙功能?将 Math.random() 的结果乘以您想要的随机生成范围,对其进行四舍五入,然后 select 使用该数字在您的数组中随机提问。
处理此问题的一种常见方法是创建一个数组来保存您需要提出的问题,随机化数组,然后在提问时从数组中删除相应的问题。然后当该数组为空时,转到您的回顾屏幕。
您可以通过以下方式之一完成此操作:
首先,让我们通过使用对象而不是一大堆数组来简化它。您的对象将具有所有相关信息的属性
//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;
}
当然,要开始工作,您只需执行以下操作:askNextQuestion()
我在 Flash 中有一个问答游戏;基本上你需要输入答案来回答 4 个问题。最后,它会显示分数和您的答案与正确答案。
- 我需要有关如何随机化问题(不重复问题)的帮助
- 最终正确答案需要与玩家回答问题的顺序相匹配
我附上了下面的图片。
代码:第 1 帧
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()}
}
第 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()}}
您是否正在寻找名为 Math.random() 的绝妙功能?将 Math.random() 的结果乘以您想要的随机生成范围,对其进行四舍五入,然后 select 使用该数字在您的数组中随机提问。
处理此问题的一种常见方法是创建一个数组来保存您需要提出的问题,随机化数组,然后在提问时从数组中删除相应的问题。然后当该数组为空时,转到您的回顾屏幕。
您可以通过以下方式之一完成此操作:
首先,让我们通过使用对象而不是一大堆数组来简化它。您的对象将具有所有相关信息的属性
//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;
}
当然,要开始工作,您只需执行以下操作:askNextQuestion()