Game Development Reference
In-Depth Information
Creating a game manager
In Dungeon Maze Adventure, all the game logic is attached to `qjcakjKja in the @qjcakjKja[I]j]can
class. If you have more than one level in your game, however, it makes sense to have some code that
exists outside of the dungeon objects so that you that can manage them (a kind of “Dungeon Master”
that isn't removed when the levels are removed).
Hey, you already know how to do this! It's called the document class! Dungeon Maze Adventure
doesn't use a document class, but why not? Go ahead and make one; it will open up all sorts of
possibilities.
You can put any properties or methods that all the dungeons can use in the document class, which
will save you having to write the same method over more than once if that method is used in more
than one level. And you can use it as a way to keep track of global values, such as a score that persists
between levels.
You can use public static methods to do this. You can define a static method using this format:
lq^he_op]pe_bqj_pekjiapdk`J]ia$%6rke`
Other classes can then access the method like this:
I]ej*iapdk`J]ia$]ncqiajpo%7
Be careful when using public methods because they can break a class's encapsulation
and open you up to some of the dependency problems you looked at in the previous
section.
You can also create getters and setters in this game manager class to allow the dungeons or the
objects in them to communicate with it. One of the dungeon objects can access the game's score with
a getter like this:
Ikrea?hel$l]najp%*o_kna
And set the score like this:
Ikrea?hel$l]najp%*o_kna'9-,7
You can also access the document class from any other class using the format Ikrea?hel$nkkp% .
You can easily access all the dungeons and objects in them from the document class like this:
`qjcakjKja*lh]uan
`qjcakjPsk*`kknPsk
You already know how to do this, so go for it!
 
Search Nedrilad ::




Custom Search