Join Todd Perkins for an in-depth discussion in this video Add sound effects, part of iOS Game Development: Swift 2.0 and SpriteKit.
- Loading audio with SpriteKit only requires one line of code. So I'm here in Xcode and I have my background file on the screen. You'll see that it's a simple loop, I'll play the file here. (music playing) Sounds like that and then I have a cannon shot and I have a hit sound. So if you have access to the exercise files you can use these. If you do not, you can find some audio files that you want to work with. One of them should be a background loop, if you can find one.
So let's go over to GameScene.swift. And the first thing we want to do is have some sort of sound play when the pegs are hit. So I'm going to go inside didHitPeg which on my code starts in line 74. And at the bottom of this method I'm going to play the hit sound. So I'll call self, run action, and playing audio is just an action. So it's going to be SKAction play sound from file named. So we pass in the name of the file which in this case is going to be hit.wave and then wait for completion.
Wait for Completion means that when we're running this action in a sequence do we go to the next action immediately or do we wait for completion. We wait until the sound file is done playing all the way. So true, we wait until a completion to move on to the next action false, if we don't. If we're not running this as part of a sequence, then the value you put there doesn't really matter anyway. So let's test this app and see what we get.
So you'll notice when I release my mouse button that there's a little bit of a stall (cannon shot) before the first sound plays. Other than that we hear the sound files playing properly. We'll address the stall later on. The reason why that's happening is because the sounds aren't preloaded. So it loads into memory right when we first call this line. So what we'll have to do is either play the sound with the volume off all the way and then turn the volume on when we want to actually hear the sound, so that it's preloaded or have some other way of loading that file into the system.
Again we'll talk about that in a later movie. So let's also copy this line of code and then scroll up to the end of touches ended and we'll play the cannon sound when we fire the cannon. There we go, hit the run button. Now we should hear the cannon sound when the cannon fires and the hit sound when the pegs are hit. And again we're going to experience that little bit of delay before the first sound is played for the first time. (peg hit) (cannon shot) (cannon shot) (peg hits) (cannon shot) (cannon shots) (peg hits) (cannon shots) So everything's working now.
So remember, when you want to play a sound file particularly a sound effect and not a song that you're going to loop in the background, you can use the SKAction, play sound filename action.
- Creating a new SpriteKit project
- Adding and modifying sprites
- Transitioning between scenes
- Removing child sprites from parents
- Working with gravity, forces, and impulses
- Detecting collisions
- Applying particle effects
- Creating actions and action sequences
- Working with audio
- Creating frame animations
- Using cameras and lights