4 回答

TA貢獻1816條經驗 獲得超4個贊
當玩家死亡時,您需要將 gameOver 設置為 True
您還需要添加邏輯以將 while 循環更改為 false 以退出 while 循環。
例如在 while 循環內設置gameOver = true
when并設置currentHealth == 0
while(gameOver == false)

TA貢獻1886條經驗 獲得超2個贊
我認為問題可能是當您調用 StopCoroutine() 時,您并沒有指示它停止協程的特定實例。相反,您需要存儲對協程的引用并將其作為參數傳遞給您的 Stop。
IEnumerator wavecoroutine = SpawnWaves();
StartCoroutine(wavecoroutine);
...
StopCoroutine(wavecoroutine);

TA貢獻1796條經驗 獲得超7個贊
ryeMoss 實際上有正確的答案。您不需要更改 while 循環的條件,而是需要確保將參考值傳遞給該StopCoroutine方法。您可以在文檔中看到這正是他們正在做的事情。
問題是什么時候SpawnWaves調用它返回一個新IEnumerator的,當你試圖阻止它時,這顯然不是你想要的,哈哈。
Start只需在您的方法內部更改為
gameOver = false;
waves = SpawnWaves(); <-- or whatever you want to call it
StartCoroutine(waves); <-- using a reference
然后傳遞waves給StopCoroutine.
始終仔細閱讀文檔;在學習新的庫、框架等時,他們是你最好的朋友。:)

TA貢獻1785條經驗 獲得超8個贊
完全修復感謝@AlexG 的指導
void Start () {
gameOver = false;
StartCoroutine (SpawnWaves());
}
IEnumerator SpawnWaves()
{
yield return new WaitForSeconds(startWait);
while (gameOver != true)
{
for (int i = 0; i < hazardCount; i++)
{
if(gameOver)break;
GameObject enemy = enemies[Random.Range(0, enemies.Length)];
Instantiate(enemy, spawnPosition1, spawnRotation1);
Instantiate(enemy, spawnPosition2, spawnRotation2);
Instantiate(enemy, spawnPosition3, spawnRotation3);
Instantiate(enemy, spawnPosition4, spawnRotation4);
Instantiate(enemy, spawnPosition5, spawnRotation5);
Instantiate(enemy, spawnPosition6, spawnRotation6);
yield return new WaitForSeconds(spawnWait);
}
yield return new WaitForSeconds(waveWait);
enemies[0].GetComponent<EnemyBehviour>().currentHealth *= eenter code herenemyHealthMultiplier;
}
}
- 4 回答
- 0 關注
- 207 瀏覽
添加回答
舉報