<< Chapter < Page Chapter >> Page >

Discussion and sample code

As usual, I will discuss the program in fragments, beginning with the Sprite class. Furthermore, I will discuss only those portions of the Sprite class that are different from the versions of the Sprite class that I explained in earlier modules.

A complete listing of the Sprite class is provided in Listing 14 and a complete listing of the Game1 class is provided in Listing 15 near the end of the module.

The Sprite class

A new Image property accessor method

Scanning down the Sprite class definition in Listing 14 , the first thing that we find that is new to this version is a new read-only Image property accessor method. The method is shown in Listing 1 .

Listing 1 . A new Image property accessor method in the Sprite class.

//Image property accessor - new to this version. public Texture2D Image {get { return image;}//end get }//end Image property accessor

There is nothing unusual about this property accessor method, so it shouldn't require further explanation. You will learn why it is needed laterwhen I explain the Game1 class.

A modified constructor

The second statement from the end in Listing 2 was added to the constructor from the previous version of the Sprite class.

Listing 2 . A modified constructor.

public Sprite(String assetName, ContentManager contentManager,Random random) { image = contentManager.Load<Texture2D>(assetName); image.Name = assetName;this.random = random; }//end constructor

The new statement assigns the assetName to the Name property of the Texture2D object that represents the sprite's image. You will see why this modification was neededlater.

A modified SetImage method

As with the modified constructor, the last statement in Listing 3 was added to the SetImage method from the previous version of the Sprite class.

Listing 3 . A modified SetImage method.

public void SetImage(String assetName, ContentManager contentManager) {image = contentManager.Load<Texture2D>(assetName); image.Name = assetName;}//end SetImage

Once again, the new statement assigns the assetName to the Name property of the Texture2D object that represents the sprite's image. You will also see why this modificationwas needed later.

A new method named GetRectangle

Listing 4 shows a new method named GetRectangle .

Listing 4 . A new method named GetRectangle.

public Rectangle GetRectangle() { return new Rectangle((int)(position.X),(int)(position.Y), image.Width,image.Height); }//end GetRectangle

This method returns the current rectangle occupied by the sprite's image as type Rectangle . This rectangle is needed for the collision detection process. The code in Listing 4 is straightforward and shouldn't require further explanation.

A new method named IsCollision

This version of the Sprite class defines a new method named IsCollision . The purpose of this new method is to detect a collision between this sprite and some other sprite.

What is a collision?

A collision is called if the rectangle containing this Sprite object's image intersects the rectangle containing a target sprite's image.

Questions & Answers

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