Jump to content

keyboard events fireing multiple times


forleafe
 Share

Recommended Posts

My code has a setup function that runs at the start. Within that setup function I have the left and right arrow keys setting up movement like so:

 

left = keyboard(37);
left.press = function(){
  moveCharacterLeft();
};

 

It works fine. But then when I get a gameover my game runs a reset code and all variables are reset back to their former states and the setup function runs again.

Well this means that the above function runs again as well, and this makes it so that each time I get a gameover and reset the game, the above function will run multiple times for each button press. So after one game over, if I hit the left key, the moveCharacterLeft() function will fire twice, and my character moves twice as far.. Three times for the next gameover.. ect..

Does anyone know how I can prevent this from happening? Admittedly I don't quite understand what's happening here..

Link to comment
Share on other sites

Sorry for the double post but this problem is really bugging me...

I've made some headway in pinpointing the issue to a piece of code that's adding listeners to the "window".

 

function keyboard(keyCode) {
  var key = {};
  key.code = keyCode;
  key.isDown = false;
  key.isUp = true;
  key.press = undefined;
  key.release = undefined;
  //The `downHandler`
  key.downHandler = function(event) {
    if (event.keyCode === key.code) {
      if (key.isUp && key.press) key.press();
      key.isDown = true;
      key.isUp = false;
    }
    event.preventDefault();
  };

  //The `upHandler`
  key.upHandler = function(event) {
    if (event.keyCode === key.code) {
      if (key.isDown && key.release) key.release();
      key.isDown = false;
      key.isUp = true;
    }
    event.preventDefault();
  };

  //Attach event listeners
  window.addEventListener(
    "keydown", key.downHandler.bind(key), false
  );
  window.addEventListener(
    "keyup", key.upHandler.bind(key), false
  );
  return key;
}

If you look down at the bottom, the event listeners are being added to the window. Perhaps they're being duplicated there every time my setup function re-runs after a game over? But I can't figure out how to dump event listeners from the friggin window!!!

Link to comment
Share on other sites

2 hours ago, forleafe said:

  //Attach event listeners
  window.addEventListener(
    "keydown", key.downHandler.bind(key), false
  );
  window.addEventListener(
    "keyup", key.upHandler.bind(key), false
  );
  return key;
}

If you look down at the bottom, the event listeners are being added to the window. Perhaps they're being duplicated there every time my setup function re-runs after a game over? But I can't figure out how to dump event listeners from the friggin window!!!

You can call window.removeEventListener() with the same parameters to remove the listener. But since bind creates a new function, I think you'd need to save a reference to the new function, so that you can pass the same function to window.addEventListener() and window.removeEventListener().

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...