-
Notifications
You must be signed in to change notification settings - Fork 1
/
scripts.js
101 lines (73 loc) · 2.88 KB
/
scripts.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// namespace object
const kkSoundboard = {
sounds: {
bees: new Audio('./assets/chasedByBees.mp3'),
DAL: new Audio('./assets/flyingToFriends.mp3'),
newBug: new Audio('./assets/newBug.mp3'),
stung: new Audio('./assets/stung.mp3'),
mainTitle: new Audio('./assets/title.mp3'),
nookMiles: new Audio('./assets/nookMilesAchievement.wav'),
item: new Audio('./assets/pickingUpItem.wav'),
furniture: new Audio('./assets/placeFurniture.wav'),
tool: new Audio('./assets/toolTransformation.wav'),
workBench: new Audio('./assets/craftingAtWorkBench.wav'),
rock: new Audio('./assets/hittingAMoneyRock.wav'),
}
};
// make init function
kkSoundboard.init = function(){
// select all lis
const buttons = document.querySelectorAll('.soundContainer button');
// run for each loop to apply the event listener
buttons.forEach(function(button){
button.addEventListener('click', (event) => {
// stop all other audio
for (let sound in kkSoundboard.sounds){
kkSoundboard.sounds[sound].pause();
}
// capture the key pressed in a variable
const buttonPressed = event.target.classList[0];
// capture the audio
const audio = kkSoundboard.sounds[buttonPressed];
// set the time to 0 to be able to spam the sound
audio.currentTime = 0;
// play the audio
audio.play();
});
// add listeners for each button to remove styles
button.addEventListener('transitionend', function(event){
event.target.classList.remove('playing');
})
})
// for trigger on key pressed
document.addEventListener('keydown', function(event){
// stop all other audio
for (let sound in kkSoundboard.sounds){
kkSoundboard.sounds[sound].pause();
}
// store the key pressed
const key = event.key;
// store the button class name for sound
const buttonToSound = document.querySelector(`button.${key}`).classList[0];
// add styles to the playing button
document.querySelector(`button.${key}`).classList.add('playing');
// capture the audio
const audio = kkSoundboard.sounds[buttonToSound];
// set the time to 0 to be able to spam the sound
audio.currentTime = 0;
// play the audio
audio.play();
})
// when click button, stop all audio
document.querySelector('.stop').addEventListener('click', function(){
// stop all audio
for (let sound in kkSoundboard.sounds){
kkSoundboard.sounds[sound].pause();
}
})
}
// document ready
document.addEventListener("DOMContentLoaded", function() {
// call initializing function
kkSoundboard.init();
});