diff --git a/articles/tutorials/building_2d_games/15_audio_controller/snippets/audiocontroller.cs b/articles/tutorials/building_2d_games/15_audio_controller/snippets/audiocontroller.cs index 34b57261..686df577 100644 --- a/articles/tutorials/building_2d_games/15_audio_controller/snippets/audiocontroller.cs +++ b/articles/tutorials/building_2d_games/15_audio_controller/snippets/audiocontroller.cs @@ -110,18 +110,18 @@ public AudioController() /// public void Update() { - int index = 0; - - while (index < _activeSoundEffectInstances.Count) + for (int i = _activeSoundEffectInstances.Count - 1; i >= 0; i--) { - SoundEffectInstance instance = _activeSoundEffectInstances[index]; + SoundEffectInstance instance = _activeSoundEffectInstances[i]; - if (instance.State == SoundState.Stopped && !instance.IsDisposed) + if (instance.State == SoundState.Stopped) { - instance.Dispose(); + if (!instance.IsDisposed) + { + instance.Dispose(); + } + _activeSoundEffectInstances.RemoveAt(i); } - - _activeSoundEffectInstances.RemoveAt(index); } } #endregion