samedi 10 octobre 2020

How to remove an event listener from window?

Im building a snake game and need to remove an event listener after the space bar has fired, but im not getting any luck as the following code;

window.removeEventListener('keypress', (event) 

doesn't do anything. Any ideas what is the proper way for this? Thanks

window.addEventListener('keypress', (event) => {
  console.log(event)
  if (event.key === ' ') {
    startGame()
    window.removeEventListener('keypress', (event))
  }
})

Aucun commentaire:

Enregistrer un commentaire