<html>
<body onkeydown="keydown(event)">
<script type="text/javascript">
event=new Object();
player=new Object();
player.x=0;
player.y=0;
function keydown(e)
{
if(window.event) // IE8 and earlier
{
event.type = "keydown";
event.key = e.keyCode;
}
else if(e.which) // IE9/Firefox/Chrome/Opera/Safari
{
event.type = "keydown";
event.key = e.which;
}
///////////////////////////////////////////////////
document.getElementById("keyp").innerHTML=event.key;
switch(event.key)
{
case 38:
player.y-=1; break;
case 40:
player.y+=1; break;
case 37:
player.x-=1; break;
case 39:
player.x+=1; break;
}
document.getElementById("px").innerHTML=player.x;
document.getElementById("py").innerHTML=player.y;
}
</script>
<div id="keyp">
Press a key in yours keyboard.
</div>
<div>
Current X: <div id="px"></div>
Current Y: <div id="py></div>
</div>
</body>
</html>
I am doing a game. First, I am debugging the events, so I can proceed with drawing to a canvas.
This code is not working. I've tried changing both javascript and HTML, with no sucess. The X displays correctly, but the Y doens't display at all. Why?
what errors are you getting? what browser are you testing with?
1. If you reply to my post, and your reply would then appear directly beneath my post, DON'T QUOTE MY ENTIRE POST!!! IT'S REDUNTANT!!! IT'S ASININE!!!! IT'S REDUNDANTLY ASININE!!!!! DON'T DO IT!!!!
2. jQuery extends the functionality of JavaScript. If you don't know JavaScript, give up on that jQuery script and learn JavaScript. You'll save yourself a lot of frustration, I promise.
3. Use the [code][/code] tags. Otherwise, you may be left wondering why no one responded to your eyesore of a thread.
in fact it should rise an error causing the function to fail -it's not allowed to write to event properties.
That is also correct, lol. I'm thinking more could have been done to self-troubleshoot. If not back to coding basics. The whole defining variables thing may be misunderstood by the looks of it
what errors are you getting? what browser are you testing with?
1. If you reply to my post, and your reply would then appear directly beneath my post, DON'T QUOTE MY ENTIRE POST!!! IT'S REDUNTANT!!! IT'S ASININE!!!! IT'S REDUNDANTLY ASININE!!!!! DON'T DO IT!!!!
2. jQuery extends the functionality of JavaScript. If you don't know JavaScript, give up on that jQuery script and learn JavaScript. You'll save yourself a lot of frustration, I promise.
3. Use the [code][/code] tags. Otherwise, you may be left wondering why no one responded to your eyesore of a thread.
Bookmarks