Unity 中的数字向导游戏不会在第一次点击时更新猜测
Number Wizard game in Unity is not updating the guess on the first click
现在我正在尝试在 Unity 4.6.9 中构建一个数字向导游戏。到目前为止,除了第一次猜测之外,一切正常。
当游戏猜到 500 时,你必须告诉它你的数字是大于、小于还是等于 500。理想情况下,如果你告诉它你的数字大于或小于它应该立即猜一个新数字(更高的为 750,更低的为 250)。
问题是它不会立即改变猜测。
当我告诉游戏数字大于最初的猜测时,Unity 中的控制台会这样显示:
- 你的号码大于500吗?
- 大于则按Y,小于则按N,为500则按E。
- 是高于还是低于500?
- 是高于还是低于750?
- 是高于还是低于875?
问题出在第3行,应该问"Is it higher or lower than 750?",然后第4行应该问"Is it higher or lower than 875?",以此类推
我真的不确定我在代码中做错了什么。如果有人愿意看一下并指出我的错误,我将不胜感激。
using UnityEngine;
using System.Collections;
public class NumberWizard : MonoBehaviour {
int max = 1000;
int min = 1;
int guess = 500;
// Use this for initialization
void Start () {
max += 1;
print ("Welcome to Number Wizard.");
print ("To begin, pick a number in your head, but don't tell me what it is.");
print ("The highest number you can pick is " +max +", and the lowest number you can pick is " +min +".");
print ("Is your number greater than " +guess +"?");
print ("Press Y if it is greater, N if it is lesser, or E if it is " +guess +".");
}
// Update is called once per frame
void Update () {
string NewGuess = "Is it higher or lower than " +guess +"?";
if (Input.GetKeyDown(KeyCode.Y)) {
min = guess;
guess = (max + min) / 2;
print (NewGuess);
} else if (Input.GetKeyDown(KeyCode.N)) {
max = guess;
guess = (max + min) / 2;
print (NewGuess);
} else if (Input.GetKeyDown(KeyCode.E)) {
print ("I won!");
}
}
}
再一次,我们将不胜感激。
问题是您从未更新正在打印的 NewGuess 字符串以实际包含新计算的猜测。试试这个:
void Update () {
string NewGuess = "Is it higher or lower than " +guess +"?";
if (Input.GetKeyDown(KeyCode.Y)) {
min = guess;
guess = (max + min) / 2;
NewGuess = "Is it higher or lower than " +guess +"?";
print (NewGuess);
} else if (Input.GetKeyDown(KeyCode.N)) {
max = guess;
guess = (max + min) / 2;
NewGuess = "Is it higher or lower than " +guess +"?";
print (NewGuess);
} else if (Input.GetKeyDown(KeyCode.E)) {
print ("I won!");
}
}
或者,更简洁的解决方案是创建一个新的打印方法,以将新的猜测作为参数传递给您的 Update 方法调用。
示例:
void PrintNewGuess(int newGuess)
{
String newGuessString = "Is it higher or lower than " +newGuess +"?";
print(newGuessString);
}
现在我正在尝试在 Unity 4.6.9 中构建一个数字向导游戏。到目前为止,除了第一次猜测之外,一切正常。
当游戏猜到 500 时,你必须告诉它你的数字是大于、小于还是等于 500。理想情况下,如果你告诉它你的数字大于或小于它应该立即猜一个新数字(更高的为 750,更低的为 250)。
问题是它不会立即改变猜测。
当我告诉游戏数字大于最初的猜测时,Unity 中的控制台会这样显示:
- 你的号码大于500吗?
- 大于则按Y,小于则按N,为500则按E。
- 是高于还是低于500?
- 是高于还是低于750?
- 是高于还是低于875?
问题出在第3行,应该问"Is it higher or lower than 750?",然后第4行应该问"Is it higher or lower than 875?",以此类推
我真的不确定我在代码中做错了什么。如果有人愿意看一下并指出我的错误,我将不胜感激。
using UnityEngine;
using System.Collections;
public class NumberWizard : MonoBehaviour {
int max = 1000;
int min = 1;
int guess = 500;
// Use this for initialization
void Start () {
max += 1;
print ("Welcome to Number Wizard.");
print ("To begin, pick a number in your head, but don't tell me what it is.");
print ("The highest number you can pick is " +max +", and the lowest number you can pick is " +min +".");
print ("Is your number greater than " +guess +"?");
print ("Press Y if it is greater, N if it is lesser, or E if it is " +guess +".");
}
// Update is called once per frame
void Update () {
string NewGuess = "Is it higher or lower than " +guess +"?";
if (Input.GetKeyDown(KeyCode.Y)) {
min = guess;
guess = (max + min) / 2;
print (NewGuess);
} else if (Input.GetKeyDown(KeyCode.N)) {
max = guess;
guess = (max + min) / 2;
print (NewGuess);
} else if (Input.GetKeyDown(KeyCode.E)) {
print ("I won!");
}
}
}
再一次,我们将不胜感激。
问题是您从未更新正在打印的 NewGuess 字符串以实际包含新计算的猜测。试试这个:
void Update () {
string NewGuess = "Is it higher or lower than " +guess +"?";
if (Input.GetKeyDown(KeyCode.Y)) {
min = guess;
guess = (max + min) / 2;
NewGuess = "Is it higher or lower than " +guess +"?";
print (NewGuess);
} else if (Input.GetKeyDown(KeyCode.N)) {
max = guess;
guess = (max + min) / 2;
NewGuess = "Is it higher or lower than " +guess +"?";
print (NewGuess);
} else if (Input.GetKeyDown(KeyCode.E)) {
print ("I won!");
}
}
或者,更简洁的解决方案是创建一个新的打印方法,以将新的猜测作为参数传递给您的 Update 方法调用。
示例:
void PrintNewGuess(int newGuess)
{
String newGuessString = "Is it higher or lower than " +newGuess +"?";
print(newGuessString);
}