Game Development Reference
In-Depth Information
Fig. 5.1
The tooltip that is
shown when you hover over
the word
SpriteBatch
together methods, classes themselves can also contain declarations. An example of
a class that contains declarations is the
Balloon
class we programmed. This class de-
clares among others a variable called
spriteBatch
and a variable called
balloonPosition
.
Another example of a class that also declares variables is the
Texture2D
class, which
is used to represent a sprite:
balloon = Content.Load<Texture2D>("spr_lives");
This instruction loads a texture from a file, and stores it in a variable of type
Texture2D
.
Texture2D
is a class, just like
SpriteBatch
and
Balloon
. You can verify this
yourself by hovering over the word with your mouse pointer in the code editor. Next
to a couple of methods, values of this class also contain variables because the im-
age data have to be stored somewhere after they have been loaded. Probably these
variables are quite complicated since an image contains a lot of information. We are
not that interested in what these variables internal to this class look like. However,
we do like to know how to use the methods in this class so we can do something
with this variable of type
Texture2D
. The variable
balloon
has as a type
Texture2D
and
therefore it contains the internal variables of the class
Texture2D
, as well as the
meth-
ods
that belong to the class. Variables such as
balloon
whose type is a class, are also
called
objects
.
5.3.1 Methods and Properties Can Manipulate an Object
The
Texture2D
class has a couple of useful methods. For example, it has a method
called
SaveAsJpeg
which allows us to save the loaded sprite as a JPEG image. This
method probably contains a lot of instructions that open a file, compress the image
information in the loaded sprite according to the JPEG standard, and that save the
compressed information in the file. But which sprite should this method save to a
JPEG file if we call the method? In the
Balloon1
and
Balloon2
programs, we have
declared the following two variables (among others):
background = Content.Load<Texture2D>("spr_background");
balloon = Content.Load<Texture2D>("spr_lives");
Suppose that we would call the method
SaveAsJpeg
as follows:
Texture2D.SaveAsJpeg(...);
How can we indicate that we want to save the
balloon
image as a JPEG file, and not
the background? The
SaveAsJpeg
method does not have a parameter where we can