PDA

View Full Version : Javascript game



Fredooo
04-28-2007, 09:14 AM
Hi guys...
First i have tio say that i am a german guy... so my english isnt the best ;)

Ok my problem is this:


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

Bob90
04-28-2007, 11:03 AM
Just add the argument 'event' to the function.

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;
}

mburt
04-28-2007, 12:20 PM
Gecko browsers (FireFox, Safari) depend an argument for these things, oftenly referred to as "e":

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.