Game Development Reference
In-Depth Information
24.2.1 Creating and Filling a Library
Creating a library in Visual Studio is easy. The XNA Game Studio provides a
project type called 'Windows Game Library'. When you select that template and
you choose a suitable name, a library project will be added to the solution. Then,
you can add a reference to that library in the project by right clicking on 'Refer-
ences' in the solution explorer and selecting 'Add Reference'. If you do not want to
create a new library, but you want to use an already existing one, right click on the
solution in the solution explorer and select
Add
Existing Project...
. Then, you can
browse to the location of the already existing library project you want to add to the
solution.
Now you can add classes to the library and these classes will be accessible to
any project that has a reference to that library. We created a library called
GameM-
anagement
, which contains all the basic classes that we have developed throughout
the topic. Most of these classes should look familiar to you since they are copied
directly from the previous version of the
Penguin Pairs
game. As a result, the
Penguin-
Pairs6
program now only contains the classes that are specific to the
Penguin Pairs
game. Generic classes that can be used by any other game, such as
SpriteGameObject
,
or
InputHelper
are now located in the
GameManagement
library. As a result, we will
not have to copy the classes anymore between different projects, we simply add a
reference to the library and we are done.
→
24.2.2 Public or Private Classes
Just like methods can be public, protected or private, so can classes as a whole. Until
now, we always started our class definitions as follows:
class
GameObject : IGameLoopObject
{
// member variables, methods, properties
}
Defining a class like this means that the class is defined as
internal
. This means that
the class can only be accessed by other code in the same assembly, but not from
another assembly. As a result, if we would have defined the
GameObject
class like
that in the
GameManagement
library, we could only use it in that library, and not
outside of it. If we want to be able to access the class in the
PenguinPairs6
project,
we need to make that class
public
. We can do this by placing the
public
keyword in
front of the class definition, as follows:
public class
GameObject : IGameLoopObject
{
// member variables, methods, properties
}