2017-06-21 44 views
0

我有一個閃光測驗遊戲;基本上你需要輸入答案來回答4個問題。最後,它會顯示你的分數和你的答案與正確的答案。如何隨機化actionscript3中的測驗問題?

  1. 我需要關於如何隨機化問題(不重複的問題)
  2. 對正確答案需要匹配,玩家回答問題的順序幫到底。

我附上下面的圖片。

end game screen

代碼:第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()}} 

回答

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()

+0

當我試過這段代碼我得到這個錯誤TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。 \t at Untitled_2_Scene1_fla :: MainTimeline/askNextQuestion() \t at Untitled_2_Scene1_fla :: MainTimeline/frame1() 什麼是q應該是在questions_txt.text = q.question;並在完成功能q.correct? – maggotypeach

+0

q是第一位的拼寫錯誤(應該是'curQuestion'而不是'q')。在我的回答結尾處的每個循環中,q代表循環當前迭代的問題對象(儘管我看到我忘記將它放在'var'的前面)。我更新了答案以修復兩個輸入錯誤/錯誤。 – BadFeelingAboutThis

+0

關於你的問題中的新信息(自我回答)。 'questions'數組將按照所要求的順序排列。 (在把'questions.pop'改爲'questions.shift'後)所以當測驗完成時,你可以迭代每個問題來回顧一下 - 我使用痕跡做了一個例子(在測試/運行時在輸出窗口中顯示AnimateCC/FlashPro – BadFeelingAboutThis

0

您是否在尋找漂亮的功能叫Math.random()?將Math.random()的結果乘以所需的隨機生成範圍,將其舍入,然後使用數字在數組中選擇一個隨機問題。