Game Development Reference
In-Depth Information
software design where input handling has to be done in the HandleInput method
and not elsewhere, because that is the only place where there is access to the
inputHelper object. Of course, there are ways around it, but the programmer
has to do extra work for that. Similarly, the spriteBatch object is only available
in the Draw method. We do not want the programmer to draw things on the
screen in the Update method: it is not intended for that by design.
Define variables as static sparingly. They can be useful if it is an variable
containing an object that is accessed from many different places and it is
completely logical that there is only a single instance of that object in your
program. In all other cases, do not define a variable as static.
7.6 Objects and Types
7.6.1 Copy by Value or by Reference
Before we finish this chapter, let us have a look at how objects and variables are
actually dealt with in memory. When dealing with basic types such as int or float ,
the variables are directly associated with a place in memory. For example, look at
the following instructions:
int i = 12;
After this instruction has been executed, the memory will look like this:
Now, we can create a new variable j , and store the value of variable i in that
variable:
int j=i;
The memory now looks like this:
If we assign another value to the j variable, for example by executing the instruc-
tion j=24 , this is what happens in memory:
Now let us have a look at what happens when we use variables of a more com-
plicated type, such as the Cannon class. Look at the following code:
Search WWH ::




Custom Search