【问题标题】:Changing value from 0 to 1 in unityscript and unity3D [closed]在 unityscript 和 unity3D 中将值从 0 更改为 1 [关闭]
【发布时间】:2013-07-27 13:58:54
【问题描述】:

如何在不得到相同值的情况下随机化这组数组?我不知道我做得对不对。它如何识别变化时的价值?

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");
    }
}

【问题讨论】:

  • 你应该缩进代码sn-p,并有更好的标题,并为Unity和使用的语言添加标签......
  • 您好,欢迎来到 Stack Overflow;这不是 100% 清楚你在问什么,记住我们不知道你的代码库。用简单的英语来说,你到底想达到什么目的?
  • 或者你可以这样做@hyde;你有代表和语言在问题中......很高兴对新人很好。
  • 如果数组索引已被选取,我正在尝试将值从 0 更改为 1,而 [index][5] == 0 的值将继续,但如果它相等到 1 它必须再次随机化才能选择另一个索引
  • 感谢您的编辑

标签: arrays unity3d unityscript


【解决方案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
    }

}

这些都是未经测试的代码,可以从一些优化中受益,但它应该可以帮助您入门。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 2019-10-28
    相关资源
    最近更新 更多