Add Sound Effects

Creating an immersive game experience can make the difference between a game that users play versus one that they toss into the trash. One way to keep players interested is to incorporate sound effects that tap into their emotions. For example, the sound effects you’ll be using for Gloop Drop are included with the project source files, but you also have the option of sourcing your own sound assets, as you saw in Gather Your Resources.

Your first order of business is to add the sound effects resources you’ll use with Gloop Drop.

Adding Resources for the Sound Effects

In keeping with the idea of an organized project, you’ll create a separate group to hold the sound resources. Drag the resources/Sounds folder into the Project Navigator, as shown in the image.

images/JuicingYourGamesWithSoundAndEffects/spritekit-addsound-resources.png

When prompted, ensure that the Copy items if needed and Add to targets options are both checked. Also, verify that the option to Create groups is selected. Once these options are set, click Finish.

When you drag the folder into the Project Navigator, Xcode will create a new group named Sounds that contains the following six files: bubbles.mp3, collect.wav, miss.wav, blob_mumble-1.wav, blob_mumble-2.wav, and blob_mumble-3.wav.

You’ll use these files to add sound effects to the game.

Creating Actions to Play Sound

Currently, when Blob catches a drop, it’s a bit underwhelming—he catches the drop and it disappears. Missing a drop is also underwhelming.

To make these events more interesting, you’ll use actions to play two sound effects: one when Blob catches a drop (collect.wav), and one when he misses a drop (miss.wav).

Open the Collectible.swift file and add the following two properties:

 private​ ​let​ playCollectSound = ​SKAction​.​playSoundFileNamed​(​"collect.wav"​,
  waitForCompletion: ​false​)
 private​ ​let​ playMissSound = ​SKAction​.​playSoundFileNamed​(​"miss.wav"​,
  waitForCompletion: ​false​)

Here, you’re creating two actions that you’ll use later to play the individual sound files. By setting the waitForCompletion parameter to false, you’re telling SpriteKit to ignore the length of the audio file and to consider the action to have completed immediately. Alternatively, you can set this option to true, making the duration of the action the same length as the audio playback.

When to Wait for Completion

images/aside-icons/tip.png

The waitForCompletion property can get a little confusing, and you may be wondering when you might consider using a value of true rather than false. One such instance would be if you’re running a sequence of actions and you need an action to run after the audio file is done playing.

With the preloaded actions in place, you’re ready to update the methods responsible for collecting and missing the drops.

Still inside the Collectible.swift file, find the collected() method and update it to match the following code:

 func​ ​collected​() {
 let​ removeFromParent = ​SKAction​.​removeFromParent​()
 let​ actionGroup = ​SKAction​.​group​([playCollectSound, removeFromParent])
 self​.​run​(actionGroup)
 }

Next, update the missed() method to match this:

 func​ ​missed​() {
 let​ removeFromParent = ​SKAction​.​removeFromParent​()
 let​ actionGroup = ​SKAction​.​group​([playMissSound, removeFromParent])
 self​.​run​(actionGroup)
 }

These changes give you some additional experience working with grouped and sequenced actions, which you first learned about in Chain Actions Together to Create a Sequence. The new grouped actions play the appropriate audio file and remove the node from the scene simultaneously.

Build and run the project, but don’t forget to turn up the volume so that you can hear the different sounds when you catch and miss the drops.

images/JuicingYourGamesWithSoundAndEffects/spritekit-build-00.png

For short, incidental sound effects—like the ones you just added—actions work well. But when you’re working with longer-running audio files, like background music or ambient noise, it’s better to use the SKAudioNode class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset