<< 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();

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