我一般是C#初学者,尝试在Unity中创建日语日语单词键入游戏,该游戏中屏幕上显示的单词/字母将以平假名显示,但请求的输入为Romaji(字母)字母。
目前,我陷入困境,试图弄清楚每个单词一次如何生成随机数。执行加(word)操作。例如,当创建Word对象时,生成一个随机数。然后,将该随机数用于依赖它的类,例如getWord_Hiragana和getWord_Romaji。互联网上存在的大多数打字游戏都只显示一个对象(英语),所以我找不到我需要的东西。
// WordManager.cs
public class WordManager : MonoBehaviour {
public List<Word> words;
public WordSpawner wordSpawner;
public void AddWord ()
{
Word word = new Word (WordGenerator.GetWord_Romaji(), wordSpawner.SpawnWord());
words.Add (word);
}
}
// WordGenerator.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WordGenerator : MonoBehaviour {
public static string[] wordList_Hiragana = { "あ", "い", "う", "え", "お" };
public static string[] wordList_Katakana = { "ア", "イ", "ウ", "エ", "オ" };
public static string[] wordList_Romaji = { "a", "i", "u", "e", "o" };
public static int GetIndex ()
{
int index = Random.Range (0, wordList_Romaji.Length - 1); // Get Random number which has the same index for Hiragana, Katakana, and Romaji arrays
Debug.Log ("Index #" + index + ": " + wordList_Hiragana[index] + " " + wordList_Katakana[index] + " " + wordList_Romaji[index]); // Debug Log
return index; // Returns the result of the random as a guidance.
}
public static string GetWord_Hiragana () // A function to return the result of GetIndex as Hiragana word to be used on WordManager and in turn, displays that Hiragana.
{
int index = GetIndex ();
string getWord_Hiragana = wordList_Hiragana [index];
return getWord_Hiragana;
}
public static string GetWord_Romaji ()
{
int index = GetIndex ();
string getWord_Romaji = wordList_Romaji [index];
return getWord_Romaji;
}
}
// Word.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Word {
public string word;
private int typeIndex; // Checks for current letter
WordDisplay display;
public Word (string _word, WordDisplay _display) // Displays the word as Hiragana / Katakana
{
word = _word;
display = _display;
display.SetWord (word);
}
public char GetNextLetter ()
{
return word[typeIndex]; // Gets the next letter of the Romaji array
}
public void TypeLetter ()
{
typeIndex++;
}
public bool WordTyped ()
{
bool wordTyped = (typeIndex >= word.Length); // Checks if the whole word has been typed
if (wordTyped)
{
display.RemoveWord (); // Remove the whole object on screen
}
return wordTyped;
}
}
预期结果是GetIndex为每个Word对象滚动随机数一次。执行getWord_Romaji时,它将获取GetIndex的返回值。执行getWord_Hiragana时也是如此。现在,GetIndex被执行两次,并在每个Word对象中两次生成一个随机数,导致Debug上出现的单词与游戏屏幕上出现的单词不同。我该如何工作?
如果上面的代码不足以解决问题,则将项目发布到here。
答案 0 :(得分:0)
在没有看到Word
的实现的情况下,我会从您的示例中说它实际上确实仅对GetIndex()
调用了一次,即GetWord_Romaji()
。 ..
无论如何,快速简单的修补程序可能是
private static int randomizedIndex;
public static void NewRandomIndex()
{
randomizedIndex = Random.Range (0, wordList_Romaji.Length - 1);
Debug.Log("Index #" + randomizedIndex + ": " + wordList_Hiragana[randomizedIndex] + " " + wordList_Katakana[randomizedIndex] + " " + wordList_Romaji[randomizedIndex]);
}
public static string GetWord_Hiragana()
{
return wordList_Hiragana[randomizedIndex];
}
public static string GetWord_Romaji ()
{
return wordList_Romaji[randomizedIndex];
}
并调用它
public void AddWord ()
{
WordGenerator.NewRandomIndex();
Word word = new Word(WordGenerator.GetWord_Romaji(), wordSpawner.SpawnWord());
words.Add (word);
}
如果您希望以后能够在这些数组之间“翻译”单词,那么您应该使用Word
而不是检索string
来存储使用过的randomizedIndex
并获取{ {1}}的价值“即时”。
答案 1 :(得分:0)
我会将您的MultibodyPlant::SetFreeBodyPose()
类构造函数更改为以下内容:
Word
请注意,我添加了另一个字段来存储为该特定单词实例选择的索引。