Game Development Reference
In-Depth Information
still left before it stops. This value will be updated every time the
Update
method is
called. Therefore, the
VisibilityTimer
class inherits from the
GameObject
class.
When the timer is created, we assume that the timer is not running, so the time
left is set to 0. We also set the total time that the timer should run to 1 second:
totaltime = 1;
timeleft = 0;
In the
Update
method, we then subtract the elapsed game time in seconds from the
timeleft
variable. If this variable contains a value less than zero, we set the target
visibility to
false
. Finally, we add a method called
StartVisible
that assigns the total
time to the
timeleft
variable.
Now we can use the
VisibilityTimer
class to control the visibility of the double and
triple combination overlays in the
Jewel Jam
game. When we create overlay objects,
we also create
VisibilityTimer
instances with these overlays as their target:
VisibilityTimer doubleTimer =
new
VisibilityTimer(doubleOverlay, 0, "doubleTimer");
this
.Add(doubleTimer);
VisibilityTimer tripleTimer =
new
VisibilityTimer(tripleOverlay, 0, "tripleTimer");
this
.Add(tripleTimer);
When the player finds two or three combinations of jewels, we start the visibility
timer of that particular overlay. For example, this is what the code looks like for the
double combination (see the
Update
method of the
JewelGrid
class):
if
(nrCombis == 2)
{
score.Score += 50;
VisibilityTimer doubleTimer = GameWorld.Find("doubleTimer")
as
VisibilityTimer;
doubleTimer.StartVisible();
}
You can see the timer in action by running the
JewelJam7
program.
18.4 A Field of Glitters
In this section, we are going to add some eye-candy to the game. Currently, the
jewels in the game are sprites displayed on the screen. We are going to add a nice
visual effect to them: glitters. Let us try to do this in a generic way: we want to be
able to designate a rectangle on the screen inside which these glitters are drawn at
random positions. We also want to be able to indicate how
dense
this rectangle of
glitters is. Then, we can create different rectangles of different sizes and attach them
to game objects in our game. So, let us create a
GlitterField
class that allows us to do
this. This class inherits from the
GameObject
class.