Book Image

Game Development with Swift

By : Stephen Haney
Book Image

Game Development with Swift

By: Stephen Haney

Overview of this book

Table of Contents (18 chapters)
Game Development with Swift
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding music and sound


SpriteKit and Swift make it very easy to play sounds in our games. We can drag sound files into our project, just like image assets, and trigger playback with SKAction playSoundFileNamed.

We can also use the AVAudio class from the AVFoundation framework for more precise audio control. We will use AVAudio to play our background music.

Adding the sound assets to the game

Locate the Sound directory in the Assets folder and add it to your project by dragging and dropping it into the project navigator. You should see the Sound folder show up in your project just like any other asset.

Playing background music

First, we will add the background music. We want our music to play regardless of which scene the player is currently looking at, so we will play the music from the view controller itself. To play the music, follow these steps:

  1. Open GameViewController.swift and add the following import statement at the very top, just below the existing import lines, to allow us access to AVFoundation...