Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What's wrong with this code?

    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>

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.

    Code:
    ch=String.fromCharCode(keycode).toLowerCase();
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #4
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    nvm guys i figured out i accidently changed the part in the body tag to onkeydown when it should be onkeypress.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •