Reputation: 2376
I have an example of what I have got so far here (example 1), you'll need to click on the 'result' window so the keys will move the character (w, a, s and d keys move the character), also bear in mind that this has not been browser tested and only works in a select few modern browsers.
I can now get the image to move up/down/left/right as wanted and depending on the 'health' it moves a particular speed, and using a good example I found it rotates along with the mouse too, so it works in a birds eye view styled game.
Now, I'm a little confused about how I can get the image to move diagonally, I've tried using multiple if
statements linking the possible combinations of keys, but it always takes the last key pressed.
I don't really know how to tackle the problem, do I need to put them in some sort of while
loop to say while a certain key is pressed move it in that direction? Would that eliminate the need for the diagonal/multiple key press?
if ((key == key_W && key == key_D) || (key == key_w && key == key_d) || (key == key_W && key == key_d) || (key == key_w && key == key_D)) {
// Player Up/Right
}
Or do I need to set some sort of array to generate a list of keys pressed?
I understand and I'm fully aware making games in Javascript/jQuery isn't the best method to make games, it's an experiment more than anything.
EDIT
There is now have a more workable, moving character here (example 2), but there is a delay in the movement, so to expand on my question, is there anyway to reduce this?
Upvotes: 4
Views: 5171
Reputation: 17894
Jwerty is a great library which handles this sort of thing, i suggest you check it out:
https://keithamus.github.io/jwerty/
It's compatible with jQuery too!
Upvotes: 3
Reputation: 39892
Well this looks like a fun project so it got me curious and I started to look around. I found an article on Quirksmode that talks about using keycode vs character code. I see that you are detecting both upper and lower case letters. The following blurb seems to indicate that this is unnecessary. This article also has important docs on browser compatibility and special key compatibility between browsers and systems. For instance, the command button on a Mac keyboard. What is it? How is it detected and behaves in various browsers?
for instance, a lower case 'a' and an upper case 'A' have the same keyCode, because the user presses the same key, but a different charCode because the resulting character is different.
http://www.quirksmode.org/js/keys.html
So then the question becomes, how do you detect multiple key presses. I found this SO answer on it that is remarkably simple and straightforward.
Can jQuery .keypress() detect more than one key at the same time?
var keys = {};
$(document).keydown(function (e) {
keys[e.which] = true;
});
$(document).keyup(function (e) {
delete keys[e.which];
})
Update
Basically I just brought in the code from above, deleted your diagonal code and iterated over the keys array for all movement operations.
http://jsfiddle.net/mrtsherman/8NW2K/3/
Upvotes: 4