Time for action – updating and drawing shots
Add the
Update()
method to the ShotManager class:public void Update(GameTime gameTime) { for (int x = Shots.Count - 1; x >= 0; x--) { Shots[x].Update(gameTime); if (!screenBounds.Intersects(Shots[x].Destination)) { Shots.RemoveAt(x); } } }
Add the
Draw()
method to the ShotManager class:public void Draw(SpriteBatch spriteBatch) { foreach (Sprite shot in Shots) { shot.Draw(spriteBatch); } }
What just happened?
Since we may be removing shots from the list, we cannot use a foreach
loop to process the list during the Update()
method. 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 has been updated, the screenBounds
rectangle is checked to see if the shot is still visible on the screen. If it is not...