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

calculate molarity of NaOH solution when 25.0ml of NaOH titrated with 27.2ml of 0.2m H2SO4
Gasin Reply
what's Thermochemistry
rhoda Reply
the study of the heat energy which is associated with chemical reactions
Kaddija
How was CH4 and o2 was able to produce (Co2)and (H2o
Edafe Reply
explain please
Victory
First twenty elements with their valences
Martine Reply
what is chemistry
asue Reply
what is atom
asue
what is the best way to define periodic table for jamb
Damilola Reply
what is the change of matter from one state to another
Elijah Reply
what is isolation of organic compounds
IKyernum Reply
what is atomic radius
ThankGod Reply
Read Chapter 6, section 5
Dr
Read Chapter 6, section 5
Kareem
Atomic radius is the radius of the atom and is also called the orbital radius
Kareem
atomic radius is the distance between the nucleus of an atom and its valence shell
Amos
Read Chapter 6, section 5
paulino
Bohr's model of the theory atom
Ayom Reply
is there a question?
Dr
when a gas is compressed why it becomes hot?
ATOMIC
It has no oxygen then
Goldyei
read the chapter on thermochemistry...the sections on "PV" work and the First Law of Thermodynamics should help..
Dr
Which element react with water
Mukthar Reply
Mgo
Ibeh
an increase in the pressure of a gas results in the decrease of its
Valentina Reply
definition of the periodic table
Cosmos Reply
What is the lkenes
Da Reply
what were atoms composed of?
Moses 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