O'Reilly logo

Swift Game Development - Third Edition by Stephen Haney, Siddharth Shekar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Playing sound effects

Playing simple sounds is even easier. We will use SKAction objects to play sounds on specific events, such as when picking up a coin or starting the game.

Adding the coin sound effect to the Coin class

First, we will add a happy sound each time the player collects a coin. To add the coin sound effect, follow these steps:

  1. Open Coin.swift and add a new property to the Coin class to cache a coin sound action:
            let coinSound =  
                SKAction.playSoundFileNamed("Sound/Coin.aif",  
                waitForCompletion: false) 
  2. Locate the collect function and add the following line at the bottom of the function to play the sound:
            // Play the coin sound: 
            self.run(coinSound) 

That is all you need to do to play the coin sound every time the player collects a coin. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required