StarField starField;
gameState
from GameStates.TitleScreen
to GameStates.Playing:
GameStates gameState = GameStates.Playing;
LoadContent()
method of the Game1 class to initialize the starField
object. Be sure to place this code after the spriteSheet
texture is loaded.starField = new StarField( this.Window.ClientBounds.Width, this.Window.ClientBounds.Height, 200, new Vector2(0, 30f), spriteSheet, new Rectangle(0, 450, 2, 2));
Upate()
method, add the following line to the GameStates.Playing
section of the switch statement you created earlier:starField.Update(gameTime);
Draw()
method, change the background color from Color.CornflowerBlue
to Color.Black
. Draw()
method, add the following line to the if
block containing GameStates.Playing:
starField.Draw(spriteBatch);
Using our StarField class is simply a matter of creating an instance of the class and then calling its Update()
and Draw()
methods during the game's corresponding methods. When the starField
object is constructed, we pass in the size of the screen, and specify that 200 stars will be created. Each star is assigned a velocity of (0, 30f)
, meaning that in one second the star will move 30 pixels downward on the screen. With a screen height of 600 pixels, each star will take 20 seconds to travel from the top of the screen to the bottom.
The spriteSheet
texture is specified, and the rectangle specifies an area on the textures that contains white pixels as we indicated above. The resulting sprite contains a single frame that is 2 by 2 pixels, located at (0, 450)
on the sprite sheet.
3.147.54.6