Time for action – updating and drawing the StarField
Add the
Update()
andDraw()
methods to theStarField
class:Public Sub Update(gameTime As GameTime) For Each star As Sprite in Stars star.Update(gameTime) If (star.Location.Y > screenHeight) Then star.Location = new Vector2( CSng(rand.Next(0, screenWidth)), 0) End If Next End Sub Public Sub Draw(spriteBatch As SpriteBatch) For Each star As Sprite in stars star.Draw(spriteBatch) Next End Sub
What just happened?
When the StarField
needs to be updated, a For Each
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...