Time for action – updating and drawing shots
Add the
Update()
method to theShotManager
class:Public Sub Update(gameTime As GameTime) For x As Integer = Shots.Count - 1 to 0 Step -1 Shots(x).Update(gameTime) If Not _screenBounds.Intersects(Shots(x).Destination) Then Shots.RemoveAt(x) End If Next End Sub
Add the
Draw()
method to theShotManager
class:Public Sub Draw(spriteBatch As SpriteBatch) For Each shot As Sprite in Shots shot.Draw(spriteBatch) Next End Sub
What just happened?
Since we may be removing shots from the list, we cannot use a For Each
loop to process the list during the Update()
method—as we saw in Flood Control, items cannot be deleted from a List
, while a For Each
loop is processing the List
. Looping backwards through the list, allows us to process all of the shots, removing ones that have expired, without the need to track separate removal lists or restart the iteration after each removal.
After an individual shot...