<< Chapter < Page
  Xna game studio     Page 5 / 8
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

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 new Sprite object

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.

List capacity considerations

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.

Assign an image to the Sprite object

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.

The overridden Update method

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 Count property of the List object

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.

The remaining 23 Sprite objects

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)) {

Questions & Answers

Explain the following terms . (1) Abiotic factors in an ecosystem
Nomai Reply
Abiotic factors are non living components of ecosystem.These include physical and chemical elements like temperature,light,water,soil,air quality and oxygen etc
Qasim
what is biology
daniel Reply
what is diffusion
Emmanuel Reply
passive process of transport of low-molecular weight material according to its concentration gradient
AI-Robot
what is production?
Catherine
Pathogens and diseases
how did the oxygen help a human being
Achol Reply
how did the nutrition help the plants
Achol Reply
Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is respiration
Deborah
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Xna game studio. OpenStax CNX. Feb 28, 2014 Download for free at https://legacy.cnx.org/content/col11634/1.6
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Xna game studio' conversation and receive update notifications?

Ask