【发布时间】:2017-03-25 18:44:10
【问题描述】:
我有一个协同程序,当切换按钮的布尔值改变时触发,当布尔值再次改变时,应该停止协同程序并启动另一个。这是我的代码:
public class thermoPowerControlPanel : MonoBehaviour {
private ThermoElectric thermo;
public bool toggleBool1;
public int temperature;
private int tempUp = 10;
private int tempDown = 1;
public thermoPowerControlPanel (){
temperature = 100;
}
public void turbine1State (bool toggleBool1) {
if (toggleBool1 == false) {
Debug.Log (toggleBool1);
Invoke("ReduceTemperatureEverySecond", 1f);
}
if (toggleBool1 == true) {
Debug.Log (toggleBool1);
Invoke("IncreaseTemperatureEverySecond", 1f);
}
}
private void ReduceTemperatureEverySecond()
{
if (toggleBool1 == true)
{
Debug.Log("I was told to stop reducing the temperature.");
return;
}
temperature = temperature - tempDown;
Debug.Log (temperature);
Invoke("ReduceTemperatureEverySecond", 1f);
}
private void IncreaseTemperatureEverySecond()
{
if (toggleBool1 == false)
{
Debug.Log("I was told to stop increasing the temperature.");
return;
}
temperature = temperature + tempUp;
Debug.Log (temperature);
Invoke("ReduceTemperatureEverySecond", 1f);
}
}
当功能turbo1State(bool t1)接收到第一个bool(false)时,程序defineTemperatureEverySecond()开始但它立即停止,发送Debug.Log消息,它应该继续降低温度直到bool(由激活切换按钮)变为真。 .
你能帮忙吗?
【问题讨论】:
-
搜索关于“停止协程”的 1000 个问题
-
我认为问题出在 bool if 条件下,我有停止它的代码,但它从未激活
-
根据 Joe Blow 的建议,我更改了代码以停止使用协同程序。
-
由于某些未知的原因,当调用 ReduceTemperatureEverySecond() 时,toggleBool1 似乎更改为 true..
-
嗨@growanimation ..你能用简单的语言解释一下,你想要实现什么那么编写代码很容易