Sound effects can add life to your games and are essential before publishing a game. This tutorial shows how sound effects can be added in SpriteKit.
- [Instructor] In Spritekit you can actually add a sound effect in scene if you want, so you can filter for sound, and you'll see play sound file named action. Now this is good if you're running an action that has something moving and it's going to play a sound file at a certain point in the animation, but if you want to play a sound file upon a code event, then you're going to need to do that in code. For example let's say we want to have the player spaceship shoot at the enemy with a laser, and then the enemy blows up afterwards and we want to have an explosion sound.
So I've imported a few sounds into here, explosion and laser, and another one called music they we'll use later on, and the goal is to have the explosion play at the right time, and the laser play when the player shoots lasers. So let's head over to game scene dot swift. An I'll hide the utility area to get some more room, and then in my code I'm going to find didBegin, this is where we're handing the collision events. And in this else statement is where we handle when the explosion happens. So when the enemy gets hit, there is an explosion. So in here I want to run an action that plays an explosion sound.
So underneath, where I create the explosion, I'm going to call self dot run, and then the action is going to be skaction, dot play soundfile name. So we'll run that, then we pass in the sound file, so this is going to be explosion. Then I'm going to tab over to wait for completion, and I'm going to set this to be false, and I'll close out the parentheses. Now wait for completion, does not apply when we're not running an action in a sequence.
What this does is it says to wait for the completion of the sound effect playing, to go to the next action. So if you want to do something after this sound effect plays when you're doing it in a sequence, then you set wait for completion, to true. Otherwise it will play the sound and go immediately to the next action before the sound is done. So at this point we should be able to test the app, and then shoot at the ship, and then when the ship gets shot, it'll blow up, and make a sound effect.
So here we go: (explosion sound) and there's the sound effect! You may have noticed there's a slight delay before the sound effect triggers, so the game actually stops for second while the sound loads up. I'm going to look at how to manage that later on in this chapter. So let's say now that we want to fire lasers with a sound effect as well. I'll copy this line of code, scroll on down to the line of code that spawns the lasers, and then at the bottom of the spawn laser method, I'll paste the code, change explosion to laser, and then run it again.
Now this time we should hear the sound effects every time a laser is shot from the ship. (pew pew pew, explosion, pew pew pew) And it's working. So if you want to play a sound in your code, simply run the action; play sound file name, passing in the appropriate file name, and then set wait for completion, if you're running this in a sequence.
- Creating a SpriteKit project
- Adding and modifying sprites
- Creating sprites in code
- Transitioning between scenes
- Working with children and parents
- Adding physics
- Detecting and responding to collisions
- Applying particle effects
- Creating actions
- Working with sound
- Adding cameras and lights