<< 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 phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
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
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
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