Game Development Reference
In-Depth Information
Balloon game = new Balloon();
Here you see that we create an object of the class Balloon , which we then manipulate
with the following instruction:
game.Run();
We call the Run method on the game object. The main thing task of this method
is calling the game loop methods. It will call the LoadContent method once, and
after that the Update and Draw methods sixty times per second. So, through the Run
method, the object called game is manipulated.
Just like methods, properties also need an object. For instance, when we store the
mouse position in the balloonPosition variable, we are using the properties X and Y
which read information from the currentMouseState object:
balloonPosition = new Vector2(currentMouseState.X, currentMouseState.Y);
There also exist properties that do not manipulate an object, such as Vector2.Zero .
This is a static property belonging to Vector2 , so it does not need an object of type
Vector2 .
5.3.2 A Class Is a Blueprint for an Object
We have seen that it is possible to create an object of a certain class type. Just like we
can declare and initialize multiple integer values, we can also declare and initialize
multiple objects of the same class:
background = Content.Load<Texture2D>("spr_background");
balloon = Content.Load<Texture2D>("spr_lives");
In this example, the Load<Texture2D> method creates the Texture2D objects for us and
we store them in two variables. We can also create objects ourselves, like we do in
the case of the SpriteBatch class:
spriteBatch = new SpriteBatch(GraphicsDevice);
When we initialize an object of a certain class, we can also say that we have cre-
ated an instance of the class. In this example, we have created an instance of the
SpriteBatch class, and this instance is called spriteBatch . Whenever we want to explic-
itly create an instance of a class, we need to use the new keyword.
You could say that a class describes what an object looks like, or: a class is the
blueprint for an object. Or to use a cooking analogy: a class is a recipe for an object.
For example, the Texture2D class describes what an image consists of, and what
methods are available to use or manipulate an image. When we create an instance of
this class, such as balloon or background , we have an actual image stored somewhere
in memory, which we can use or manipulate with the methods available from the
Texture2D class.
Search WWH ::




Custom Search