Game Development Reference
In-Depth Information
We have seen already quite a few examples of properties. For instance, the
Content property inside the Game class. This property gives us an object of type
ContentManager , which in turn has a property RootDirectory that can be written to.
Another example is the GraphicsDevice property, which gives us an object of type
GraphicsDevice and we can use that object to set a background color, or to pass as a
parameter for creating a spritebatch:
spriteBatch = new SpriteBatch(GraphicsDevice);
Watch out: in the case of GraphicsDevice , the name of the property and the name of
the type confusingly are the same. In the coming chapters, we will be using these
properties a lot. They are a very useful tool to access the information inside an object
in a structured way.
5.3.6 Member Variables and Local Variables
Until now, we have seen that classes are a collection of methods and proper-
ties. They can also be used as a type. A variable that has a class as a type is
called an object. An object is also called an instance of class. Methods and prop-
erties manipulate the memory in some way. They can read it and write to it.
But if we have an object, which 'memory' belongs to it, and how is it manip-
ulated? This is done through member variables . We already have used member
variables in our example programs. These are the variables that were declared
at the class level and not at the method level. Member variables are like nor-
mal variables, except they belong to a class and not to a method. For exam-
ple, the spriteBatch variable in the Balloon program is a member variable of that
class.
An example of a variable that is not a member variable is the currentMouseState
variable, because it is not declared on the class level, but in the Update method.
Variables that are not member variables are also called local variables , because they
can only be used in the method that they were declared in. Another example of a
local variable is the yPosition variable in the FlyingSprites program. This variable is
declared in the Update method, and it cannot be used outside that method.
5.4 Using the Mouse Position to Rotate the Cannon Barrel
One of the features of the Painter game is that it contains a cannon barrel that rotates
according to the mouse position. We can now write the part of the program that does
this, using the tools that we have discussed in this chapter. You can see this already
workinginthe Painter1 program in the solution belonging to this chapter. We declare
a few member variables for storing the background sprite, the cannon barrel sprite,
the cannon barrel position and its origin:
 
Search WWH ::




Custom Search