Ready for some Extra Credit?
Adding a sound for every coin you collect would be pretty sweet right? Well if you’re ready to level up. Let’s adds some sound effects to our coin collection JavaScript functions!
Step 1: Update Your HTML File
First, we’ll update your HTML file once again to include a audio element with a link to our sound file. If you don’t have a sound file you can find some sample sounds here.
Once you’ve found your sound save it in the rsc > mp3 folder. Make sure to take note of the filename for your sound effect because we’ll need to place that name in our html audio element.
Add this line of code under Before the closing body element in your HTML file.
<audio id="coin-collection-sound" src="rsc/mp3/your-sound-file-name-here.mp3" type="audio/mpeg"></audio>
Step 2: Update Your JavaScript File
Go to line #603 and the following JavaScript code to your file.
// Play the coin collection sound effect
const coinSound = document.getElementById('coin-collection-sound');
coinSound.currentTime = 0; // Rewind the sound to the beginning
coinSound.play();
El Fin!
That should do it! Mind you I haven’t tested this last piece out but I know you guys got the skills!