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 binations 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?
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 binations 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?
Share Improve this question edited Oct 18, 2011 at 17:32 no. asked Oct 18, 2011 at 16:04 no.no. 2,3764 gold badges28 silver badges44 bronze badges 4- Hehe - its fun just to make the little guy run around. cool project. For people checking this out use WASD to move character and it follows mouse too. – mrtsherman Commented Oct 18, 2011 at 16:06
- As an aside I could only get this working in IE9, not Chrome or Firefox. – Andy Rose Commented Oct 18, 2011 at 16:11
- @Jleagle - you're correct, yes it does although not in Firefox. – Andy Rose Commented Oct 18, 2011 at 16:15
-
@AndyRose It's probably because I'm using the CSS3
rotate
which could be causing problems. And I haven't checked it for x-browser patibility yet. But thank you for point it out :) – no. Commented Oct 18, 2011 at 16:38
2 Answers
Reset to default 4Well 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 patibility and special key patibility between browsers and systems. For instance, the mand 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/js/keys.html
So then the question bees, 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/mrtsherman/8NW2K/3/
Jwerty is a great library which handles this sort of thing, i suggest you check it out:
https://keithamus.github.io/jwerty/
It's patible with jQuery too!