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

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
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 genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza 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