【发布时间】:2016-07-30 04:46:38
【问题描述】:
public class green : MonoBehaviour
{
private AudioSource source;
public AudioClip sound;
static int result = 0;
void Start()
{
StartCoroutine("RoutineCheckInputAfter3Minutes");
Debug.Log("a");
}
IEnumerator RoutineCheckInputAfter3Minutes()
{
System.Random ran = new System.Random();
int timeToWait = ran.Next(1, 50) * 1000;
Thread.Sleep(timeToWait);
source = this.gameObject.AddComponent<AudioSource>();
source.clip = sound;
source.loop = true;
source.Play();
System.Random r = new System.Random();
result = r.Next(1, 4);
Debug.Log("d");
yield return new WaitForSeconds(3f * 60f);
gm.life -= 1;
Debug.Log("z");
}
public void Update()
{
if (result == 1 && gm.checka == true)
{
Debug.Log("e");
StopCoroutine("RoutineCheckInputAfter3Minutes");
gm.life += 1;
source.Stop();
gm.checka = false;
Debug.Log("j");
}
if (result == 2 && gm.checkb == true)
{
Debug.Log("f");
StopCoroutine("RoutineCheckInputAfter3Minutes");
gm.life += 1;
source.Stop();
gm.checkb = false;
Debug.Log("z");
}
else if (result == 3 && gm.checkc == true)
{
StopCoroutine("RoutineCheckInputAfter3Minutes");
Debug.Log("g");
gm.life += 1;
source.Stop();
gm.checkc = false;
Debug.Log(gm.life);
}
}
}
有两个问题
如果用户在 3 分钟内没有按任何按钮,我想让音乐停止并且生命变量减少 -1。但是如果用户按下右键,life变量会增加+1。但是我不知道如何从用户那里得到空输入3分钟。
如果我在这个程序中使用
while,它会关闭……直到生命值低于 0,我想重复播放不规则时间的音乐。
【问题讨论】:
-
result是什么意思?gm是什么?你如何决定那些checka、checkb和checkc?您应该给出一个干净的代码,并删除其中不必要的部分。 -
我以为人们一下子就知道了;;;下次我会注意的;;;
标签: c# multithreading unity3d timer