Skip to content

Commit

Permalink
Add volume control to local storage, plus fix scoring bug
Browse files Browse the repository at this point in the history
  • Loading branch information
cassidoo committed Dec 30, 2018
1 parent 8336bc1 commit f82d599
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
32 changes: 29 additions & 3 deletions game.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@ let clicks = 0;
let snd;
let audio = true;

function audioSetup() {
snd = new Audio("sizzle.wav");
let volumeButton = document.getElementById('vol');

if (localStorage.getItem('volume') !== null) {
audio = localStorage.getItem('volume') == 'true';
if (audio) {
volumeButton.src = 'volume-high.svg'
} else {
volumeButton.src = 'volume-no.svg'
}
}
volumeButton.addEventListener('click', () => {
if (audio) {
volumeButton.src = 'volume-no.svg'
audio = false;
} else {
volumeButton.src = 'volume-high.svg'
audio = true;
}
localStorage.setItem('volume', audio);
});
}

function randomGrid() {
for (let i = 0; i < 5; i++) {
let row = [];
Expand Down Expand Up @@ -63,9 +87,11 @@ function handleClick(x, y) {
snd.currentTime = 0;
}

if (checkWin()) win();

addClick();
if (checkWin()) {
win();
} else {
addClick();
}
}

function win() {
Expand Down
12 changes: 1 addition & 11 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,7 @@ <h3>How to Play</h3>
<script>
(function () {
generateGrid(randomGrid());
snd = new Audio("sizzle.wav");
let volumeButton = document.getElementById('vol');
volumeButton.addEventListener('click', () => {
if (audio) {
volumeButton.src = 'volume-no.svg'
audio = false;
} else {
volumeButton.src = 'volume-high.svg'
audio = true;
}
});
audioSetup();
if (localStorage.getItem('ffbestscore') !== null) {
Array.from(document.getElementsByClassName('hide')).forEach((e) => {
e.classList.remove('hide');
Expand Down

0 comments on commit f82d599

Please sign in to comment.