我想知道为什么每次运行协程时,传递到协程中的值都会被覆盖。该值为"0";currentAmountOfEnemiesStoned";像这个private int currentAmountOfEnemiesSpawned;
一样在顶部声明
当使用协同程序删除条目时,是否有特定的内容?
谢谢!
private IEnumerator SpawnEnemy(float secondsBetweenSpawns = 1f)
{
print($"Enemies to spawn is: {totalAmountOfEnemiesToSpawn}");
currentAmountOfEnemiesSpawned += 1;
if(currentAmountOfEnemiesSpawned >= totalAmountOfEnemiesToSpawn)
{
print("stopping respawn");
StopEnemySpawning();
}
isEnemyReadyToSpawn = false;
Vector2 rndPosWithin = new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f));
rndPosWithin = transform.TransformPoint(rndPosWithin * .5f);
var spawedEnemy = Instantiate(enemyToSpawn, rndPosWithin, transform.rotation);
spawedEnemy.name = enemyToSpawn.name;
spawedEnemy.transform.parent = enemyParentGameObject.transform;
yield return new WaitForSeconds(secondsBetweenSpawns);
isEnemyReadyToSpawn = true;
}
public void StartEnemySpawning(int amountOfEnemiesToSpawn)
{
isSpawningPaused = false;
currentAmountOfEnemiesSpawned = 0;
totalAmountOfEnemiesToSpawn = amountOfEnemiesToSpawn;
StartCoroutine(SpawnEnemy(1f));
}
当您说传递时,您有一个名为currentAmountOfEnemiesStoned的类级作用域变量。它是相同的引用,因此从该类中的任何方法设置它都会更新它。
您可能需要引入一个直到currentAmountOfEnemiesStoned的for循环,该循环调用SpawnEnemy方法,而不需要任何后续代码来更新/设置变量值。
意外传入一个"1〃;以为是时候了,但我通过了1,因为有大量的敌人要繁殖。。。愚蠢的谢谢