Game Development Reference
In-Depth Information
pass along the image that needs to be saved. This is because the method manipulates
an object . For example, if we want this method to manipulate the object balloon ,we
can do this as follows:
balloon.SaveAsJpeg(...);
So instead of placing the name of the class before the dot, we place the object that
we want to manipulate before the dot. Generally we cannot choose whether we
want a method to manipulate an object, or simply call it from its class. For instance
the SaveAsJpeg method has to be called on an object. It cannot be called directly
from a class. If we would try to execute the instruction Texture2D.SaveAsJpeg(...); ,the
compiler would complain that the SaveAsJpeg method has to be called on an object
(apart from the missing parameter values). Another example of a method that has
to be called on an object is the Draw method from the SpriteBatch class. And here is
another example:
spriteBatch.Begin();
Here we call the Begin method on the object spriteBatch . Just like the SaveAsJpeg
method, the Begin method has to be called on an object. If we call the method without
specifying which object it should manipulate, we get a compiler error:
SpriteBatch.Begin();
You cannot just call any method on any object. The Begin method is defined in the
SpriteBatch class, so it can only manipulate objects of type SpriteBatch .Tryingto
have the Begin method manipulate a Texture2D object, like balloon , would result in a
compiler error:
balloon.Begin();
To conclude, some methods manipulate objects, some methods do not. We have
seen quite a few examples of the latter. For instance, the GetState method from the
Mouse class does not need an object, we call it directly from the class. When a
method does not manipulate an object but we call it directly from the class it is
defined in, the method is called static . The method Main in the Balloon1 and Balloon2
programs is static, and you can see that it is static because the header of this method
contains the keyword static . Since it is static, the Main method does not manipulate
an object. The reason that the Main method has to be static is because it is the method
that is executed when the program starts, and at that time no objects have been
created yet, so there is no object to manipulate.
If you look at the Draw method in the Balloon class, you see that this method
is not static (the static keyword is not written in its header). This means that this
method manipulates an object. But which object does it manipulate? It manipulates
the object that we created in the Main method:
Search WWH ::




Custom Search