Game Development Reference
In-Depth Information
We create the alert view as before, but here we set its delegate to our scene.
The following is the
delegate
method:
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
[self configureConnectedGameControllers];
}
}
When you press a button in the alert view, and if it has the delegate set, it will call
this method on its delegate. The button's index starts at zero, and zero is the cancel
button that is always present. If there are any other buttons, they will have the
subsequent indexes.
Usually, if we have multiple alert views, we have to understand which alert view
called what, and what the button at said index should do.
But in our situation, we have only one alert view that has more than one button,
and we know what it is, so we don't actually need to check anything here. If the
user tapped the
Yes
button on the alert view, we will configure all the connected
controllers. If they click on
No
, which is at index
0
, nothing will happen.
Adding sound and music
Our game feels a bit bland without sound effects, so we are going to add some.
We will add background music and sound effects.
Sprite Kit is mostly a graphics library, and sound support is limited, but there are
ways to add music to our game.
The first thing that we need to do is add music files to the project. To do this,
drag-and-drop
Kick_Shock.mp3
,
shieldCharged.wav
, and
shieldSmashed.wav
to our project in Xcode. Select
Copy items into destination group's folder
and
make sure that
Add to targets
has your project selected.
In order to play background music, we will need to add the
AVFoundation
library
to our project.
The
AVFoundation
library is a collection of classes that allows for audio and video
playback on iOS devices. To add
AVFoundation
to our project, add the
@import
AVFoundation;
line to
ERGMyScene.h
. We will also need a new property to play
music. To do this, add the following property to
ERGMyScene.h
:
@property (strong, nonatomic) AVAudioPlayer* musicPlayer;