<< Chapter < Page | Chapter >> Page > |
Listing 6 . The modified constructor for the Game1 class.
public Game1() {//constructor
graphics = new GraphicsDeviceManager(this);Content.RootDirectory = "Content";
//Set the size of the game window.graphics.PreferredBackBufferWidth = 450;
graphics.PreferredBackBufferHeight = 450;}//end constructor
You have seen code identical to this code in earlier modules so there is nothing new to discuss here.
The overridden LoadContent method is shown in Listing 7 .
Listing 7 . The overridden LoadContent method.
protected override void LoadContent() {
spriteBatch = new SpriteBatch(GraphicsDevice);//Create the first sprite in the LoadContent
// method using the noarg constructor.sprites.Add(new Sprite());
//Assign an image to the sprite.sprites[0].SetImage("blueball",Content);}//end LoadContent
A statement near the center of Listing 7 instantiates a new Sprite object with no image and calls the Add method of the generic List object to add the object's reference to the end of the list.
One advantage of using a generic List object as an alternative to a simple array is that it is not necessary to declare thecapacity of the List object when the program is compiled. The capacity of the List object increases automatically as necessary to accommodate all of the references that are added to the list.
Since this is the first reference added to the list, it can be accessed later using an index value of 0.
The last statement in Listing 7 retrieves the reference from the list at index 0 and uses that reference to call the SetImage method on the Sprite object to which it refers.
You learned about the SetImage method in the discussion of the code in Listing 3 . This call causes the Sprite object whose reference is stored at index 0 in the list to load the image from the image filewith the Asset Name property value of "blueball".
As mentioned earlier, the second parameter named Content is a reference to a ContentManager object that is inherited from the Game class.
As you learned earlier , this program creates, manipulates, and draws 24 objects of the Sprite class in the game window as shown in Figure 2 . The first Sprite object was created in the overridden LoadContent method when it was called earlier.
The generic List object referred to by sprites has a property named Count that keeps track of the number of references contained in the object. The first time the Update method is called, the value of Count is 1 as a result of the Sprite object's reference having been added to the list in the LoadContent method earlier.
This program creates the remaining 23 sprites in the Update method to simulate a game in which sprites come and go as the game progresses.
The overridden Update method begins in Listing 8 .
Listing 8 . Beginning of the overridden Update method.
protected override void Update(GameTime gameTime) {
if(sprites.Count<(maxSprites)) {
Notification Switch
Would you like to follow the 'Xna game studio' conversation and receive update notifications?