如何在不获得相同值的情况下随机化这组数组?我不知道我做得对。如何在变化时识别价值?
function Start ()
{
if(MasterVar.quiz<10)
{
var englishLecture = [
["Word that tells us about an action","Verb","Noun","Pronoun","a","0"],
["Name of person, place or things","Verb","Noun","Cute","b","0"],
["Select the noun","Swim","Shout","Joe","c","0"],
["There __ a cat","is","was","can","a","0"],
["You __ go home now","can","open","was","a","0"],
["She ___ her breakfast","ate","play","sleep","a","0"],
["Select the verb","John","is","table","b","0"],
["We ___ planning to go to the movies tonight","were","could","can","b","0"],
["What ____ that noise?","Were","Was","Can","b","0"],
["What ___ your name?","are","can","is","c","0"]
];
index = Random.Range(0,englishLecture.length);
while(englishLecture[index][5] == "1")
{
index = Random.Range(0,englishLecture.length);
}
englishLecture[index][5] == "1";
TextGUI = GameObject.FindGameObjectWithTag("Question");
TextGUI.GetComponent(GUIText).text = englishLecture[index][0];
sel1 = englishLecture[index][1];
sel2 = englishLecture[index][2];
sel3 = englishLecture[index][3];
rightAns = englishLecture[index][4];
}
else if(MasterVar.quiz>=10)
{
Application.LoadLevel("Score");
}
}
答案 0 :(得分:1)
我假设“0”/“1”值用于存储先前是否询问过问题,并且while循环用于选择新的随机问题,直到找到未答复的问题。
您已经将值从“0”更改为“1”,但主要问题是,一旦设置为“1”,您再也不会再读它了。
如果您希望应用程序记住问题被标记为已解答(“1”),则需要将该数据写入本地文件,并在应用程序启动时从该文件中读取数据。您需要了解文件I / O和序列化才能做到这一点: http://msdn.microsoft.com/en-us/library/vstudio/ms172873.aspx
如果您在询问第一个随机问题后只需要提出另一个问题,那么在第一个问题被标记为已回答后,您需要重复询问问题的代码。您可以通过使用while循环,事件或将问题代码封装到函数中来实现。这都是关于结构的!
例如,你想要离开这个:
index = Random.Range(0,englishLecture.length);
while(englishLecture[index][5] == "1") {
index = Random.Range(0,englishLecture.length);
}
englishLecture[index][5] == "1";
TextGUI = GameObject.FindGameObjectWithTag("Question");
TextGUI.GetComponent(GUIText).text = englishLecture[index][0];
sel1 = englishLecture[index][1];
sel2 = englishLecture[index][2];
sel3 = englishLecture[index][3];
rightAns = englishLecture[index][4];
//Add code to manage user interaction here
......对此:
for (var i = 0; i < 5; i++) { // This will ask 5 questions
AskRandomQuestion();
}
function AskRandomQuestion() {
index = Random.Range(0,englishLecture.length);
while(englishLecture[index][5] == "1") {
index = Random.Range(0,englishLecture.length);
}
englishLecture[index][5] == "1";
TextGUI = GameObject.FindGameObjectWithTag("Question");
TextGUI.GetComponent(GUIText).text = englishLecture[index][0];
sel1 = englishLecture[index][1];
sel2 = englishLecture[index][2];
sel3 = englishLecture[index][3];
rightAns = englishLecture[index][4];
//Add code to manage user interaction here
}
另外,请注意,当所有问题都被标记为已回答时,while子句将无限循环,永远不会找到未回答的问题,并且应用程序将崩溃。
更好的设计是将所有未回答的问题提取到一个新数组中(创建一个仅包含未回答问题的数组)。如果该数组不为空,您可以在其中选择一个随机问题。否则,您知道所有问题都已得到解答。
类似的东西:
function AskRandomQuestion() {
unasweredQuestions = new Array();
for (var i = 0; i < englishLecture.length; i++) {
if (englishLecture[i][5] == "0") {
unansweredQuestions.push(englishLecture[i]);
}
}
if (unansweredQuestions.length > 0) {
selectedQuestion = unansweredQuestions[Random.Range(0,unansweredQuestions.length)]
selectedQuestion[5] == "1";
TextGUI = GameObject.FindGameObjectWithTag("Question");
TextGUI.GetComponent(GUIText).text = selectedQuestion[0];
sel1 = selectedQuestion[1];
sel2 = selectedQuestion[2];
sel3 = selectedQuestion[3];
rightAns = selectedQuestion[4];
// Add code to manage user interaction here
} else {
// All questions were answered, quit or reset
}
}
这是所有未经测试的代码,可以从一些优化中受益,但它应该可以帮助您入门。