<< Chapter < Page | Chapter >> Page > |
The parameters required by this method and the body of the method are the same as for the first constructor discussed above.
Listing 4 shows the Draw method of the Sprite class in its entirety.
Listing 4 . The Draw method of the Sprite class.
public void Draw(SpriteBatch spriteBatch) {
spriteBatch.Draw(texture,position,Color.White);}//end Draw method
//-------------------------------------------------//}//end Sprite class
}//end namespace
This method should be called after a call to the SpriteBatch.Begin method and before a call to the SpriteBatch.End method.
The method requires a single parameter, which is a reference to the SpriteBatch object on which the Begin method has been called.
You should recognize the single statement in the method as a call to the simplest available Draw method belonging to the SpriteBatch object. This version of the SpriteBatch.Draw method allows the caller to specify
Listing 4 signals the end of the Sprite class.
Methods of the Game1 class were overridden to demonstrate the use of the Sprite class to produce the output described earlier .
The class named Game1 begins in Listing 5 .
Listing 5 . Beginning of the class named Game1.
namespace XNA0126Proj {
public class Game1 : Microsoft.Xna.Framework.Game {GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;//References to the Sprite objects are stored in this
// List object.List<Sprite>sprites = new List<Sprite>();
int maxSprites = 24;//Max number of sprites.int frameCnt = 0;//Game loop frame counter
//This is the limit on the number of frames in which// the sprites are moved.
int moveLim = 200;
Listing 5 simply declares several instance variables, most of which you should recognize. The others are well described by the comments. However,one of the instance variables, sprites , introduces a concept that is new to this module.
Listing 5 declares a variable named sprites and populates it with a reference to a new generic List object that is conditioned to store and retrieve references to objects of the class Sprite .
Here is some of what the documentation has to say about the generic List class:
The generic List class provides many more capabilities than I will use in this program. I will use an object of the generic List class to store references to Sprite objects that I can later access using a zero-based index. This will eliminate the requirement to declarea separate reference variable for each of the Sprite objects that I instantiate.
The constructor for the Game1 class was modified to set the size of the game window as shown in Listing 6 .
Notification Switch
Would you like to follow the 'Xna game studio' conversation and receive update notifications?