-
Notifications
You must be signed in to change notification settings - Fork 53
/
sound.js
71 lines (62 loc) · 1.95 KB
/
sound.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Sound {
constructor(parent){
this.parent = parent;
this.sounds = [];
this.muted = true;
}
create(src, id, loop = false){
let audio = document.createElement("audio");
audio.src = src;
audio.id = id;
audio.muted = true;
this.sounds.push(audio);
this.parent.append(audio);
if(loop){
audio.setAttribute("loop", "")
}
return audio;
}
}
Sound.prototype.soundSetting = function(){
let soundItems = document.querySelectorAll(".sound-item");
for(let soundItem of soundItems){
soundItem.addEventListener("click", (e)=>{
this.muteToggle();
});
}
};
Sound.prototype.muteToggle = function(){
if(!this.muted){
for(let sound of this.sounds){
sound.muted = true;
}
document.querySelector("#sound-speaker").innerHTML = "\u{1F507}";
document.querySelector("#sound-description").innerHTML = "off";
this.muted = true;
}else{
for(let sound of this.sounds){
sound.muted = false;
}
document.querySelector("#sound-speaker").innerHTML = "\u{1F509}";
document.querySelector("#sound-description").innerHTML = "on";
this.muted = false;
}
};
Sound.prototype.pause = function(){
for(let sound of this.sounds){
sound.pause();
}
}
Sound.prototype.play = function(){
for(let sound of this.sounds){
sound.play();
}
}
let sound = new Sound(document.querySelector("#sound-div")),
backgroundSound = sound.create("assets/sounds/Dungeon_Theme.mp3", "background_sound", true),
movesSound = sound.create("assets/sounds/moves.mp3", "moves_sound"),
dropSound = sound.create("assets/sounds/drop.mp3", "drop_sound"),
pointsSound = sound.create("assets/sounds/points.mp3", "points_sound"),
finishSound = sound.create("assets/sounds/finish.mp3", "finish_sound");;
sound.muteToggle();
sound.soundSetting();