...

View Full Version : What's wrong with this code?



cgwalls
09-26-2008, 01:37 AM
Hey everyone well i'm working on this super simple javascript game where when you press a key a player gets a point and there's two players. the game was working perfectly until i had to restore my computer. For some reason it won't work anymore maybe it's because norton came preloaded on the computer? So i was hoping you guys could see if there's anything wrong with it.
<html>
<head>
<title>Score Battle</title>
<style>
#playingArea{
position: absolute;
border: 1px solid black;
width: 500;
height: 400;
background-color: rgb(192,192,192);
}
#player1{
position: absolute;
top: 50;
left: 180;
}
#player2{
position: absolute;
top: 50;
left: 280;
}
#div1{
position: absolute;
top: 100;
left: 200;
}
#div2{
position: absolute;
top: 100;
left: 300;
}
#won{
position: absolute;
top: 150;
left: 150;
}
</style>
<script type="text/javascript">
var a = 0;
var b = 0;
function add1() {
a += 1;
document.getElementById("div1").innerHTML = a;
}
function add2() {
b += 1;
document.getElementById("div2").innerHTML = b;
}
function ProcessKeypress(e)
{
if (e.keyCode) keycode=e.keyCode;
else keycode=e.which;
ch=String.fromCharCode(keycode);

if(ch=='q') add1();
else if(ch=='p') add2();
}
</script>
</head>
<body onKeyDown="ProcessKeypress(event);">
<div id="playingArea">
Controls:<br />Player 1: Q<br />Player 2: P
<div id="player1">
Player 1
</div>
<div id="player2">
Player 2
</div>
<div id="div1">
0
</div>
<div id="div2">
0
</div>
</div>
</body>
</html>

_Aerospace_Eng_
09-26-2008, 03:00 AM
If you alert ch you will see that its upper case. I think to avoid the way different browsers interpret keystrokes you should convert ch to lowercase.


ch=String.fromCharCode(keycode).toLowerCase();

cgwalls
09-28-2008, 04:58 AM
well i did what you told me to and it didn't work. i just don't understand why it won't work now. it was on the web before my computer was wiped out and it worked fine. Now it won't even work on other computers and i didn't even change anything on the the file hoster. How would my comuters memory getting erased effect what can be displayed on other computers through the internet!

cgwalls
09-28-2008, 05:35 AM
nvm guys i figured out i accidently changed the part in the body tag to onkeydown when it should be onkeypress.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum