Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Key Listener Help

    I am in the process of designing a simple game where the player flies a ship around using the arrow keys. I currently use a series of switch statements to execute certain actions dependant upon which key(s) is(are) pressed. Here is the code I use:

    Code:
    window.addEventListener('keydown', function (event) {
    		switch (event.keyCode) {
    		case arrowLeft: //left
    			player.moveLeft = true;
    			player.vr = -1;
    			break;
    		case arrowRight: //right
    			player.moveRight = true;
    			player.vr = 1;
    			break;
    		case arrowUp: //up
    			player.moveUp = true;
    			player.thrust = thrust;
    			break;
    		case arrowLeft && arrowUp:
    			player.moveLeft = true;
    			player.vr = -1;
    			player.moveUp = true;
    			player.thrust = thrust;
    			break;
    		case arrowRight && arrowUp:
    			player.moveRight = true;
    			player.vr = 1;
    			player.moveUp = true;
    			player.thrust = thrust;
    			break;
    		case arrowRight && arrowLeft && arrowUp:
    			player.moveUp = true;
    			player.thrust = thrust;
    			break;
    		}
    	}, false);
    	
    	// Keyup input listeners
    	window.addEventListener('keyup', function (event) {
    		switch (event.keyCode) {
    		case arrowLeft: //left
    			player.vr = 0;
    			player.moveLeft = false;
    			break;
    		case arrowRight: //right
    			player.vr = 0;
    			player.moveRight = false;
    			break;
    		case arrowUp: //up
    			player.thrust = 0;
    			player.moveUp = false;
    			break;
    		}
    	}, false);
    The problem I am having is that the key presses don't always seem to register. For example, the player cannot always smoothly switch from a right turn quickly to a left turn. Sometimes he can, but oftentimes it seems like the key press is not always registered and the player must mash the key a second time to finally get it to register. Is there a more reliable method for listening for and registering key presses vs. using a switch statement or a bunch of if statements (which gave me the same result)?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,590
    Thanks
    0
    Thanked 644 Times in 634 Posts
    See if substituting keypress instead of keydown helps - keydown can be triggered by keys that don't return a keycode
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Do I just swap 'keydown' with 'keypress'? When I do that it no longer registers any key presses.

  • #4
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I found the problem and it had nothing to do with the keypress listener. The keys register just fine. Sorry about the confusion.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •