<< Chapter < Page | Chapter >> Page > |
Note in Figure 1 that the sprite sheet has a white non-transparent background. The game window is also caused to have a white background so that the whitebackground of the rectangle containing the sprite image can't be distinguished from the game window background.
The overridden Draw method begins in Listing 10 .
Listing 10 . Beginning of the overridden Draw method.
protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.White);//Background
The statement in Listing 10 erases everything in the game window by painting over it with a solid white background.
You learned in an earlier module that the drawing process consists of a minimum of three statements.
The first statement is a call to the SpriteBatch.Begin method. This is followed by one or more calls to the SpriteBatch.Draw method. This is followed by a call to the SpriteBatch.End method.
There are four overloaded versions of the SpriteBatch.Begin method. The parameters for the different versions establish drawing parametersthat apply to all of the subsequent calls to the SpriteBatch.Draw method until there is a call to the SpriteBatch.End method.
This program uses the simplest version of the SpriteBatch.Begin method with no parameters as shown in Listing 11 . This version simply:
Listing 11 . Begin the drawing process.
spriteBatch.Begin();
This program makes a single call to the SpriteBatch.Draw method followed by a call to the SpriteBatch.End method each time the Game.Draw method is called.
There are several overloaded versions of the SpriteBatch.Draw method and the one shown in Listing 12 is one of the most complex. It was necessary to use this version to cause the sprite to be scaled by the value ofthe variable named scale when it is drawn.
Listing 12 . Call the SpriteBatch.Draw method.
spriteBatch.Draw(myTexture,//sprite sheet
spritePosition,//position to draw//Specify rectangular area of the
// sprite sheet.new Rectangle(
xStart,//Upper left corneryStart,// of rectangle.
frameWidth, //Width and heightframeHeight),// of rectangle
Color.White,//Don't tint sprite0.0f,//Don't rotate sprite
//Origin of sprite. Can offset re// position above.
new Vector2(0.0f,0.0f),//X and Y scale size scale factor.
new Vector2(scale,scale),spriteEffect,//Face correctly
0);//Layer numberspriteBatch.End();
The nine parameters required for this version of the method areidentified in the documentation as shown below. (Note that square brackets were substituted for angle brackets in the following list to avoid problems increating the cnxml format required for the OpenStax website.)
Notification Switch
Would you like to follow the 'Xna game studio' conversation and receive update notifications?