I am playing around with making a 2d fighter game. using only javascript, css and html I am stuck on how to prevent the attack button to be spammed like crazy. If i press the attack key 5 times per seconde it also registers and also does the animation. I want to restrict the key register to 1 time per 750 milliseconds.
How would i accomplish this? I think it has something to do with the timeStamp but i cant work out the logic
This is what i have without the restriction:
window.addEventListener('keydown', (event) =>{
switch (event.key) {
case ' ':
if (player_1.position.x < (player_2.position.x + 25))
{player_1.attack_player_1()
// setTimeout(() => {sword_swing.play()},200)}
setTimeout(() => {sfx_player_1.sword_swing.play()},200)}
else {player_1.attack_player_1_reversed()
setTimeout(() => {sfx_player_1.sword_swing.play()},200)
}
break
}
});