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

Draw new material every 83 milliseconds

The code in Listing 6 causes the sprites to be drawn sixty times per second, but new material is drawn only once every msPerFrame milliseconds. The value for msPerFrame is set to 83 milliseconds in Listing 1 , but as you learned in an earlier module, its value could be changed by program code as the program is running.

Therefore, in this program, new material is drawn 30 times per second. Every other frame that is drawn looks exactly like the one before it, but the humaneye is not fast enough to be able to detect that.

Apply dynamic scaling to the background image

A different and ever increasing scale factor is applied to the background image each time the body of the if statement in the Update method in Listing 6 is executed.

Two components in the scale factor

This overall scale factor is composed of the fixed backgroundBaseScale factor computed in Listing 5 and a dynamic scale factor named dynamicScale that is computed in Listing 7 .

An optical illusion

This is what causes the size of the planet to continue to increase during the animation sequence creating the illusion that the UFO is getting closer to thesurface of the planet.

The dynamic portion of the overall scale factor

The value of the dynamic portion of the overall scale factor increases by 0.03 each time this code is executed. Once the dynamic portion reaches a valueof 10, it is reset to zero and the animation sequence plays again from the beginning.

This is accomplished by the scaling algorithm shown in Listing 7 .

Listing 7 . Apply dynamic scaling to the background image.

//Reset the animation if the dynamicScale factor // is greater than 10.if(dynamicScale>10) { dynamicScale = 0.0f;} else { //Increase the dynamicScale factor and use it// to compute a new scale factor that will be // applied to the background image in the next// call to the Draw method. dynamicScale += 0.03f;backgroundScale = backgroundBaseScale * (1 + dynamicScale);}//end if-else on dynamicScale

Changing the origin of the background image

The SpriteBatch.Draw method has one parameter named position and another parameter named origin . You can cause the origin to be any point in the image relative to its upper left corner.

Important: The position parameter specifies the position in the game window where the image's origin will be drawn.

If you hold the position parameter constant and vary the origin , the image will appear to slide around within the game window.

Increasing the scale alone is insufficient

Consider the background image shown in Figure 3 . Assume that the origin of the image is the upper left corner of the image. Also assume that the image will be drawn with that origin in the upper left corner of the game window.

If we were to scale the image to make it larger, the planet would be pushed down and to the right and would exit the game window near the bottom rightcorner of the game window. That is not what we are looking for.

Need to stabilize the location of the planet in the game window

To achieve the effect we are looking for, we need to cause the planet to remain in pretty much the same location within the game window as it getslarger. One way to accomplish that is by causing the origin to move down and to the right within the background image as the planet becomes larger. (Obviouslythat is not the only way to accomplish it.)

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