<< Chapter < Page
  Xna game studio     Page 7 / 9
Chapter >> Page >

That effect is accomplished by the algorithm shown in Listing 8 .

Listing 8 . Adjust the origin for the background image.

//Also use the dynamicScale factor to compute a // new value for the origin of the background.// The origin is the point in the image that is // drawn at the point in the game window that is// passed as the second (position) parameter in // the call to the SpriteBatch.Draw method. This// has the effect of causing the background // image to slide up and to the left at the same// time that it is getting larger. spaceOrigin =new Vector2((float)(450 * (dynamicScale) / 12),(float)(338 * (dynamicScale) / 10)); }//end if on msElapsedbase.Update(gameTime); }//end Update

Examples

For example, in Figure 4 , the origin of the image (the point drawn in the upper left corner of the game window) was out in space far removed from theplanet. By Figure 10 the origin had shifted to the surface of the planet causing a point on the surface of the planet to be drawn in the upper left corner of thegame window. By Figure 12 , the origin had shifted all the way down and to the right to be on the surface of the ring that surrounds the planet.

The end of the Update method

Listing 8 signals the end of the overridden Update method.

The overridden Game.Draw method

To avoid confusion, I want to remind you that when programming in XNA, you override the Game.Draw method. Within that method, you make calls to the SpriteBatch.Draw method. These are entirely different methods belonging to different objects even though they have the samename.

No need to clear the game window

The overridden Game.Draw method begins in Listing 9 . Unlike the programs in earlier modules in this book, there is no need to clear thegame window to a constant color in this program because the background image of the planet completely fills the game window.

Two main sections of code

In this program, the overridden Game.Draw method consists of two main sections of code. The first section consists of a Begin , Draw , End sequence with alpha blending turned off. This code is used to draw the background image. Alpha blending is turnedoff to prevent any areas of the background image that may have a low alpha value or any green pixels from appearing to be transparent.

The second section consists of another Begin , Draw , End sequence with alpha blending turned on. This code is used to draw the UFO. Alphablending is turned on to cause the green areas in the UFO sprite shown in Figure 2 to be transparent and to let the background image show through.

Draw the background image showing the planet

The first major section of code is shown in Listing 9 .

Note the change that was made in the call to the SpriteBatch.Begin method to upgrade the program from XNA 3.1 to XNA 4.0.

Listing 9 . Beginning of the overridden Game.Draw method.

protected override void Draw(GameTime gameTime) { // Turn off blending to draw the planet in the// background. Note the update for XNA 4.0. // spriteBatch.Begin(SpriteBlendMode.None);spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque); //Draw the background.spriteBatch.Draw(spaceTexture,//sprite Vector2.Zero,//position re windownull,//rectangle Color.White,//tint0,//rotation spaceOrigin,//originbackgroundScale,//scale SpriteEffects.None,1.0f);//layer, near the back spriteBatch.End();

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