We all know that music is very important in films as it helps immerse viewers in the story, and this happens in video games too. Depending on the game, you will need to transmit different emotions and highlight actions or situations. Luckily, SpriteKit provides us with a group of utilities to play and handle sounds, making these tasks very simple.
Let's start by creating a new group on the project navigator and adding the needed audio resources:
Right-click on the InsideTheHat group on the navigator, select New Group, and call it Sound.
Right-click on the Sound group and select Add Files to "InsideTheHat"….
In the
7338_02_Resources
folder, you will findinsidethehat_background.mp3
,wrong_door.mp3
, andcorrect_door.mp3
. Select them and click on Add.
We will start by playing the background music for our game so that it can be more interesting. We will need a variable to handle the music. Add the following line after the score
variable declaration:
private var backgroundMusic:SKAction...