<< Chapter < Page | Chapter >> Page > |
Sifting through all of this detail in an attempt to get a big picture view, we see that we should:
Listing 5 shows the declaration of two instance variables followed by the constructor for the Game1 class. One of the instance variables is used in the constructor and the other is used later in theprogram.
Listing 5 . Constructor for the Game1 class.
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;public Game1() {
graphics = new GraphicsDeviceManager(this);Content.RootDirectory = "Content";
}// end constructor
A constructor is a special method-like structure that is executed once and only once during the instantiation of an object.
The first statement in the Main method in Listing 2 uses the new operator to cause the constructor to be executed. When the constructor completes its task, it returns a reference to theobject just constructed. That reference is stored in the local reference variable of type Game1 named game in Listing 2 .
The first statement in the constructor in Listing 5 instantiates a new object of the class GraphicsDeviceManager and stores that object's reference in the instance variable named graphics .
The documentation for GraphicsDeviceManager isn't very descriptive. Here is some of what Aaron Reed (the author of the Learning XNA books from O'Reilly) has to say on the topic.
He goes on to explain how the GraphicsDevice object acts as a conduit between your XNA program and the physical graphics device on yourmachine.
Note the parameter that is passed to the GraphicsDeviceManager constructor in Listing 5 . The documentation tells us that the parameter must be of type Game and is the Game that the GraphicsDeviceManager should be associated with.
I don't recall having discussed the keyword this earlier in this collection of modules. According to Jesse Liberty ( Programming C# from O'Reilly)
Notification Switch
Would you like to follow the 'Xna game studio' conversation and receive update notifications?