Update()
and Draw()
methods to the StarField class:public void Update(GameTime gameTime) { foreach (Sprite star in stars) { star.Update(gameTime); if (star.Location.Y > screenHeight) { star.Location = new Vector2( rand.Next(0, screenWidth), 0); } } } public void Draw(SpriteBatch spriteBatch) { foreach (Sprite star in stars) { star.Draw(spriteBatch); } }
When the star field needs to be updated, a foreach
loop processes each item in the stars
list, running the sprite's Update()
method. The method then checks the star's Location
property's Y
component to determine if the star has moved off the bottom of the screen. If it has, the star is assigned a new Location
with a random X component and a Y component of zero, placing the star at a random location along the top of the screen.
The StarField.Draw()
method simply passes along the spriteBatch
object to each of the individual stars in the stars
list and instructs them to draw themselves.
18.117.158.47