在我的代码中,我想实现一个计时器,它使死亡等待2秒才能初始化。
void OnCollisionEnter2D(Collision2D other)
{
Die();
}
void Die()
{
Application.LoadLevel(Application.loadedLevel);
}
死亡是即时的,我希望它在初始化之前等待2秒。
有什么想法吗?
答案 0 :(得分:4)
如果您只是希望它在两秒钟后发生,您可以试试这个 -
void OnCollisionEnter2D(Collision2D other)
{
Invoke ("Die", 2.0f);
}
void Die()
{
Application.LoadLevel(Application.loadedLevel);
}
答案 1 :(得分:0)
某处用2000初始化一个计时器并定义一个处理程序,如下所示:
//...
Timer tmr = new Timer();
tmr.Interval = 2000; // 20 seconds
tmr.Tick += timerHandler;
tmr.Start(); // The countdown is launched!
//...
private void timerHandler(object sender, EventArgs e) {
//handle death
}
//...
答案 2 :(得分:-1)
试试这个:
void OnCollisionEnter2D(Collision2D other)
{
Thread Dying = new Thread(()=>Die());
Dying.Start(); //start a death in new thread so can do other stuff in main thread
}
void Die()
{
Thread.Sleep(2000); //wait for 2 seconds
Application.LoadLevel(Application.loadedLevel);
}