<< 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 mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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