Still new at this, have been searching and searching. I see some answers but the code looks a bit different from mine using var and a few others. I found this code to use.
function throttle(cb, delay = 2000) {
let shouldWait = false
return (...args) => {
if (shouldWait) return
cb (...args)
shouldWait = true
setTimeout(() => {
shouldWait = false
}, delay)
}
}
To my understanding that part is correct. I have the basic movement keys. I only want to throttle the jump keypress. Case 87: any help would be greatly appreciated. Trying to avoid players tapping the jump button to fly everywhere. I've tried other alternatives but throttling would solve my problem.
addEventListener('keydown', ({ keyCode }) => {
if (game.disableUserInput) return
var lastEnter = null;
switch (keyCode) {
case 65:
console.log('left')
keys.left.pressed = true
lastKey = 'left'
break
case 83:
console.log('down')
break
case 68:
console.log('right')
keys.right.pressed = true
lastKey = 'right'
break
case 87: // W
console.log('up')
if (event.repeat) { return }
player.velocity.y -= 25
break