在我的游戏中,当玩家死亡时,会播放死亡的声音,一旦声音结束,当用户还有足够的生命时,应该重新加载场景。
在我听到声音之前,在调用death()函数时,游戏立即停止了:
public static void Death()
{
AddCoinScript.coinCounter = 0;
LivesScript.livesCounter--;
if (LivesScript.livesCounter > -1)//to get 0 live
{
Debug.Log("TIMER");
var currentScene = SceneManager.GetActiveScene();
SceneManager.LoadScene(currentScene.name);
}
else
{
//TO DO GameOver
}
}
这很管用。
public static void Death()
{
AddCoinScript.coinCounter = 0;
LivesScript.livesCounter--;
PlayDeathSound();
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = aSDeath.clip.length * 1000;
timer.Start();
timer.Elapsed += delegate
{
timer.Stop();
if (LivesScript.livesCounter > -1)//to get 0 live
{
Debug.Log("TIMER");
var currentScene = SceneManager.GetActiveScene();
SceneManager.LoadScene(currentScene.name);
}
else
{
//TO DO GameOver
}
};
}
如你所见,为了确保计时器真正启动,我设置了一个“调试日志(“定时器”)“看看,如果它真的工作。你猜怎么着:是的。调试现在在控制台中显示“TIMER”。但你知道什么不再管用了吗?下面的两行代码。
var currentScene = SceneManager.GetActiveScene();
SceneManager.LoadScene(currentScene.name);
当我把它全部改回来时,它又起作用了。只有当计时器触发这两行时,它们才会被忽略。
这太奇怪了还是我遗漏了什么?谢谢您!