Game Development Reference
In-Depth Information
The
playBg
method internally preloads the audio file before playing it. With looping en-
abled, the audio track will loop indefinitely. The drawback of playing audio this way is
that you can't specify a
seekTime
.
Let's complete the music playback by opening
SceneManager.m
and locating its
presentGameScene
method. In order to play the game music, add the code high-
lighted in
Listing 11-3
to the end of the method.
Listing 11-3
.
Playing game music when presenting the game scene
-(void) presentGameScene
{
// existing code omitted for brevity ...
OALSimpleAudio* audio = [OALSimpleAudio sharedInstance];
[audio playBg:@"Audio/game-music.m4a" loop:YES];
}
If you play the game now, you should hear music playing in the menu and different music
when you play the game.
You may want to improve this behavior—for instance, to have the game music track start
over if you advance from one level to another or restart the level. One way to fix that is to
extend the
presentGameScene
method in
SceneManager
with an additional para-
meter of type
BOOL
—for instance,
presentGameS-
ceneWithMusic:(BOOL)playMusic
. You would only pass in
YES
for
playMusic
when presenting the game scene from the main menu; otherwise, you would
pass
NO
to that parameter. Inside the method, add a condition to play the music only when
playMusic
is
YES
.
Playing Sound Effects
Assuming you'll want to play a sound effect every time a popover layer is closed, you can
do so easily by adding the code highlighted in
Listing 11-4
at the end of the
shouldClose
method found in
CloseButton.m
.
Listing 11-4
.
Playing a sound effect when closing popovers
-(void) shouldClose
Search WWH ::
Custom Search