【发布时间】:2023-04-02 10:22:01
【问题描述】:
我正在写一个有趣的文字冒险,我有一个角色能够从河里钓鱼的场景。我试图让他一次钓到几条鱼,但是当我运行代码时,Random.Range 方法会继续运行,直到它超过我的阈值。我尝试了几个循环,看看是否可以让它运行一次,但没有这样的运气。
我确定我遗漏了一些明显的东西,但我一直遇到同样的问题。首先我尝试了一个 for 循环,下面的代码我使用了一个 do while 循环,但都没有效果。非常感谢您的帮助。
这是我遇到问题的代码:
public void catch_fish()
{
if (fish > 1000)
{
text.text = "Rock-Smasher already has a lot of fish! You should cook those first! \n\n " +
"Press Space to back to the river";
if (Input.GetKeyDown(KeyCode.Space))
{
currentState = States.river;
}
}
else
{
int amount = 0;
amount = Random.Range(0, 7);
fish = fish + amount;
text.text = "You peer down into the water and you quickly thrust your hand into the water, catching " + fish + " fish. Wow! \n\n " +
"Press R to go back to the river, or F to catch more fish";
if (Input.GetKeyDown(KeyCode.R))
{
currentState = States.river;
}
else if (Input.GetKeyDown(KeyCode.F))
{
currentState = States.catch_fish;
}
}
}
【问题讨论】:
-
如果您使用的是 C# Random 类,您应该能够执行以下操作
Random rnd = new Random();amount = rnd.Next(1, 7); -
Input.GetKeyDown看起来像 Unity3d,如果这是 Unity3d,您必须将标签添加到您的问题中作为编辑。 -
你想要一种让游戏等到用户按下按钮的方式?此代码不会实现这一点。 catch_fish 运行了多少次?我看不出你这样做的目的。它不会循环,对吧?虚拟 = 0,虚拟++,而 (1
-
我删除了循环,我只设置了它来尝试让 catch_fish 只运行一次。我遇到的问题是 catch_fish 继续运行,直到数量超过阈值,我不知道如何解决这个问题,所以它只运行一次。