我有一個閃光測驗遊戲;基本上你需要輸入答案來回答4個問題。最後,它會顯示你的分數和你的答案與正確的答案。如何隨機化actionscript3中的測驗問題?
- 我需要關於如何隨機化問題(不重複的問題)
- 對正確答案需要匹配,玩家回答問題的順序幫到底。
我附上下面的圖片。
代碼:第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()}}
當我試過這段代碼我得到這個錯誤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
q是第一位的拼寫錯誤(應該是'curQuestion'而不是'q')。在我的回答結尾處的每個循環中,q代表循環當前迭代的問題對象(儘管我看到我忘記將它放在'var'的前面)。我更新了答案以修復兩個輸入錯誤/錯誤。 – BadFeelingAboutThis
關於你的問題中的新信息(自我回答)。 'questions'數組將按照所要求的順序排列。 (在把'questions.pop'改爲'questions.shift'後)所以當測驗完成時,你可以迭代每個問題來回顧一下 - 我使用痕跡做了一個例子(在測試/運行時在輸出窗口中顯示AnimateCC/FlashPro – BadFeelingAboutThis