Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-30-2012, 10:09 PM   PM User | #1
BrianDP1977
New to the CF scene

 
Join Date: Aug 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
BrianDP1977 is an unknown quantity at this point
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)?
BrianDP1977 is offline   Reply With Quote
Old 04-30-2012, 10:42 PM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,449
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 04-30-2012, 10:54 PM   PM User | #3
BrianDP1977
New to the CF scene

 
Join Date: Aug 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
BrianDP1977 is an unknown quantity at this point
Do I just swap 'keydown' with 'keypress'? When I do that it no longer registers any key presses.
BrianDP1977 is offline   Reply With Quote
Old 04-30-2012, 11:05 PM   PM User | #4
BrianDP1977
New to the CF scene

 
Join Date: Aug 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
BrianDP1977 is an unknown quantity at this point
I found the problem and it had nothing to do with the keypress listener. The keys register just fine. Sorry about the confusion.
BrianDP1977 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:02 AM.


Advertisement
Log in to turn off these ads.