jeudi 1 janvier 2015

Stabilizing the fire frequency

I've added onkeydown event listener to document, and when the event fires, I play an audio file. This audio file is a gun sound. If you tap the key once, it plays fine, but if you press and hold, the audio repeats incredibly fast. I've solved that with a simple playing condition check, but I have another problem. If I press and hold, only after the first shot, it sounds like the gun is firing repeatedly. For example, it goes like ta-tatatatata.


How can I fix my machine gun? Make it fire like tatatata.


Demo





var weapons = {
aug : {
audio : "weapons/aug-1.wav",
icon : "e",
key : "A"
}
};

function playAudio(file) {
if (!playing) {
var audio = new Audio(file);
audio.play();
playing = true;
setTimeout(function() {
playing = false;
}, 100);
}
}

document.onkeydown = function(e) {
switch(e.keyCode) {
case 65:
playAudio(weapons.aug.audio);
break;
}
}

Aucun commentaire:

Enregistrer un commentaire