Game Development Reference
In-Depth Information
16.2.6 Updating the RowSelectGameObject Class
We can now give the playing grid an identifier “grid”, so that later on, we can find
the grid again in the game objects that use it:
JewelGrid grid = new JewelGrid(10, 5, 0, "grid");
playingField.Add(grid);
Let us update our existing classes so that they use the identifier system. As an ex-
ample of how to do this, we will look at the RowSelectGameObject class. This class
needs to access the grid object because it has to apply a row shifting operation to it.
Because we can retrieve the grid object based on its identifier, we do not have to pass
it along to the constructor anymore or store it in a member variable. For example in
the HandleInput method, we can retrieve the grid object as follows:
JewelGrid grid = GameWorld.Find("grid") as JewelGrid;
Here, the GameWorld property comes in handy. We use it transparently to retrieve
the grid by calling the Find method. Using that method, we can find any game object
that has a unique ID, which is very useful for handling input and updating the state
of game objects that depend on other game objects.
As an extra advantage, the RowSelectGameObject class has become simpler, since
we do not need to store the grid as a member variable anymore. We can simply look
for it when we need it. For the new version of the row selection game object class,
see Listing 16.1 .
16.3 Introducing a Couple of New Game Object Types
16.3.1 The ScoreGameObject Class: Maintaining the Current
Score
The next step in making this game more interesting is to add a few game objects that
relate to the way that the game is played and how rewards are handed to the player.
In this game, we express the reward given to the player by a number of points: the
score . Every time the player finds a valid combination of symbols, the player gains
10 points. This current score should be stored in a variable or an object somewhere.
Also, the score should be written on the screen, so that the player knows how many
points he/she currently has obtained.
Now we see another advantage of not specifically assuming that every game ob-
ject is represented by a sprite. The 'score' game object uses a font to display itself
on the screen. To make this even a bit more generic, we are first going to introduce a
class called TextGameObject , which simply writes some text on the screen at a certain
position. This class is very similar to the SpriteGameObject class, except that we draw
text on the screen instead of a sprite. In order to do that, we need to store the text to
 
Search WWH ::




Custom Search