Results 1 to 3 of 3

Thread: Javascript game

  1. #1
    Join Date
    Apr 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Javascript game

    Hi guys...
    First i have tio say that i am a german guy... so my english isnt the best

    Ok my problem is this:
    Code:
    function pushed() 
    { 
    keyval=0 
    keyval=event.keyCode 
    if (keyval==37) { ballmoves[0]=-1; } 
    if (keyval==39) { ballmoves[0]=1; } 
    score-=1 
    if (score<0) score=0 
    document.gamefield.scoretxt.value=score 
    return; 
    }
    This code only works in IE and Opera... (netscape i dont know)
    But it doesnt work in FireFox...

    Can someone explain me why?
    I know that Firefox interpret

    event.keycode
    as
    Event.keycode

    but how can i put in a "switch"
    so that the game is playable at all Browser?!

    PLz can someone help me?!
    THX Fredooo

  2. #2
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default

    Just add the argument 'event' to the function.
    Code:
    function pushed(event) 
    { 
    keyval=0 
    keyval=event.keyCode 
    if (keyval==37) { ballmoves[0]=-1; } 
    if (keyval==39) { ballmoves[0]=1; } 
    score-=1 
    if (score<0) score=0 
    document.gamefield.scoretxt.value=score 
    return; 
    }

  3. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Gecko browsers (FireFox, Safari) depend an argument for these things, oftenly referred to as "e":
    Code:
    function pushed(e) 
    { 
    var events = e || event;
    var keyval = events.keyCode;
    if (keyval==37) { ballmoves[0]=-1; } 
    if (keyval==39) { ballmoves[0]=1; } 
    var score = -1; 
    if (score<0) score=0 
    document.gamefield.scoretxt.value=score 
    return; 
    }
    Using the "var" keyword is necessary as well.
    - Mike

Bookmarks

Posting Permissions

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