我在编码时遇到问题。当我回到Unity并玩游戏时,基于文本的游戏开始在" Wake Up"按照指示的场景。但是,游戏对按下的任何按键都没有响应。场景永远不会改变,它会停留在"唤醒"除非我手动到sNextHeading选项卡并输入不同的场景名称。例如,我输入" Check Woven Basket"那场景加载。我知道我的所有场景都有效,但它们对按键没有反应。我在学校看了我的讲座,看起来很相像。我必须忽略一些事情,我希望有人能指出我正确的方向。
Page[] aBook;
public string sPrevHeading;
public string sCurrHeading;
public string sNextHeading = "Wake Up";
void Start()
{
BindBook();
RenderStory();
}
void Update() //Input Keys Don't Work
{
if (Input.GetKeyDown(KeyCode.W))
{
if (sCurrHeading == "Wake Up")
{
sNextHeading = "Check Woven Basket";
}
}
else if (Input.GetKeyDown(KeyCode.D))
{
if (sCurrHeading == "Wake Up")
{
sNextHeading = "Check Door";
}
else if (sCurrHeading == "Pick Up")
{
sNextHeading = "Drink Milk";
}
}
else if (Input.GetKeyDown(KeyCode.B))
{
if (sCurrHeading == "Wake Up")
{
sNextHeading = "Check Bowl";
}
}
else if (Input.GetKeyDown(KeyCode.P))
{
if (sCurrHeading == "Bowl")
{
sNextHeading = "Pick Up";
}
else if (sCurrHeading == "Pick Up")
{
sNextHeading = "Pour Milk";
}
}
else if (Input.GetKeyDown(KeyCode.Y))
{
if (sCurrHeading == "Drink Milk" ||
sCurrHeading == "Pour Milk")
{
sNextHeading = "Wake Up";
}
}
else if (Input.GetKeyDown(KeyCode.N))
{
if (sCurrHeading == "Drink Milk" ||
sCurrHeading == "Pour Milk")
{
sNextHeading = "Thank You";
}
}
RenderStory();
}
void RenderStory()
{
if (!string.IsNullOrEmpty(sNextHeading))
{
for (int nPage = 0; nPage < aBook.Length; nPage++)
{
if (sNextHeading == aBook[nPage].Heading)
{
sCurrHeading = sNextHeading;
sNextHeading = "";
Debug.Log(aBook[nPage].Body);
}
}
}
}
void BindBook()
{
aBook = new Page[]
{
new Page("Wake Up", "(Wake Up)\nCheck the [W]oven Basket, [B]owl, or check the [D]oor?"),
new Page("Check Woven Basket", "(Woven Basket)\n[X] to return to previous step."),
new Page("Check Bowl", "(Check Bowl)\n[P]ick up bowl of mile or [X] to return."),
new Page("Check Door", "(Check Door)\n[X] to return to previous step."),
new Page("Pick Up", "(Pick Up)\n[P]our out the milk, [D]rink the liquid or [X] to return to previous step."),
new Page("Pour Milk", "(Pour Milk)\nWin Play again [Y]es or [N]o?"),
new Page("Drink Milk", "(Drink Milk)\nLose Play again [Y]es or [N]o?"),
new Page("Thank You", "(Thank You)\nThank you for playing!")
};
}
}
答案 0 :(得分:0)
我认为当您开始游戏时,sCurrHeading
实际上是null
:它在此代码段中说明了这一点:
public string sCurrHeading;
public string sNextHeading = "Wake Up";
将其更改为
public string sCurrHeading = "Wake Up";
public string sNextHeading;
可能有帮助。
因为它是null
,所有这些检查(例如if (sCurrHeading == "Wake Up")
)都会失败,而else
语句也会失败。
答案 1 :(得分:0)
好吧所以我一直在尝试每个人都建议的所有有用选项,我找到了解决问题的方法。我试图搜索的关键词与我在数组中设置的不匹配。例如,&#34;碗&#34;与#34;检查碗。&#34;我正在检查的标题没有与我创建的标题完全匹配。但是,我写下了所有这些可能的修复,以防我将来遇到问题。再次感谢你!