Game Development Reference
In-Depth Information
Why do we calculate this vector in the LoadContent method instead of for example
in the Draw method? Here we touch upon a very important issue when programming
games: efficiency . We can safely assume that the size of the balloon sprite will not
change while the game is running. Therefore, it would not make sense to recalculate
the bottom center of the image more than once. If we would calculate this vector in
the Draw method, we would perform this calculation about sixty times per second
as long as the game is running. In this example, it would probably not affect the
performance of the application that much because the calculation is not that com-
plicated. However, once your games start to become more complex, it is crucial to
calculate things at the right place and that we are not wasting computation power
when it is not needed. Therefore, we calculate the bottom center of the sprite in the
LoadContent method, which is executed only once.
We can now use the nine-parameter version of the Draw method to draw the bal-
loon at the origin that we calculated:
spriteBatch.Draw(balloon, balloonPosition, null , Color.White,
0.0f, balloonOrigin, 1.0f, SpriteEffects.None, 0);
5.3 Classes, Types, and Objects
Let us reconsider some of the terms that have been introduced. We have seen that
instructions are grouped into methods, and methods are grouped into classes. We
have already seen quite a few examples of what a method looks like and that one
of the instruction forms is calling a method. We have also discussed the difference
between an instruction and an expression . An instruction changes memory, an ex-
pression has a value. For example, have a look at the following expression:
Mouse.GetState()
What happens here is that we call the method named GetState from the Mouse class.
We said in the previous section that the result of calling this method is the current
mouse state, which we can store in a variable. The type of that variable has to be
MouseState . Let us look at another example of an expression:
new SpriteBatch(GraphicsDevice)
The result of this expression is a 'spritebatch' thing, which we can store in a vari-
able of type SpriteBatch . But what does this mean exactly? If you hover your mouse
pointer over the word SpriteBatch in the Visual Studio editor, you see a tool tip indi-
cating that SpriteBatch is actually a class! This is also shown in Fig. 5.1 .
Apparently, SpriteBatch is a class, but at the same time it is also a type . It seems
that classes are more than just 'collections of methods'. Classes can be used as a
type, and we can declare and initialize variables of that type. And next to grouping
Search WWH ::




Custom Search