我在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()}}
答案 0 :(得分:2)
处理此问题的常用方法是创建一个数组来保存需要提问的问题,随机化数组,然后在询问时从数组中删除相应的问题。然后,当该数组为空时,请转到回顾屏幕。
以下是您可以实现此目标的众多方法之一:
首先,让我们通过使用对象而不是一大堆数组来简化这一过程。您的对象将具有所有相关信息的属性
//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()
答案 1 :(得分:0)
您是否正在寻找名为Math.random()的漂亮功能?将Math.random()的结果乘以所需的随机生成范围,将其四舍五入,然后使用数字在数组中选择一个随机问题。