Game Development Reference
In-Depth Information
public void
PlayMusic(
string
assetName,
bool
repeat =
true
)
{
MediaPlayer.IsRepeating = repeat;
MediaPlayer.Play(contentManager.Load<Song>(assetName));
}
Now we can simply add sound effects and music wherever we like. When the
game is started, we start playing the background music in the
JewelJam
class, as
follows:
assetManager.PlayMusic("snd_music");
And when we get a valid combination of jewels (single, double or triple), we play
different sound effects (see the
JewelGrid
class):
if
(nrCombis == 1)
JewelJam.AssetManager.PlaySound("snd_combi");
else if
(nrCombis == 2)
{
score.Score += 50;
VisibilityTimer doubleTimer = GameWorld.Find("doubleTimer")
as
VisibilityTimer;
doubleTimer.startTimer();
JewelJam.AssetManager.PlaySound("snd_double");
}
else if
(nrCombis == 3)
{
score.Score += 100;
VisibilityTimer tripleTimer = GameWorld.Find("tripleTimer")
as
VisibilityTimer;
tripleTimer.startTimer();
JewelJam.AssetManager.PlaySound("snd_triple");
}
Finally, we play a sound when the game is over (see the
JewelJamGameWorld
class):
if
(
this
.GameOver && !gameover.Visible)
{
gameover.Visible =
true
;
JewelJam.AssetManager.PlaySound("snd_gameover");
}
This completes the
Jewel Jam
game. You can play the game by running the
JewelJam7
example project belonging to this chapter. Happy jewel hunting!