views:

872

answers:

4
<script>
var personX = 18;
var personY = 13;

function processArrowKeys(E) {
    if (E.keyCode == 37 || E.keyCode == 38 || E.keyCode == 39 || E.keyCode==40) {
        E.preventDefault();
    }

    if (E.keyCode == 37) {
        if (currentterrain[personX - 1][personY] == 0 || currentterrain[personX - 1][personY] == 1 || currentterrain[personX - 1][personY] == 3) {
            personX--;
        }
    }
    if (E.keyCode == 39) {
        if (currentterrain[personX + 1][personY] == 0 || currentterrain[personX + 1][personY] == 1 || currentterrain[personX + 1][personY] == 3) {
            personX++;
        }
    }
    if (E.keyCode == 38) {
        for (i = 0; i < 3; i++) {
            if (currentterrain[personX][personY - 1] == 0 || currentterrain[personX][personY - 1] == 1 || currentterrain[personX][personY - 1] == 3) {
                personY--;
            }
        }
    }
}
</script>

<body onkeydown="processArrowKeys(event)">

The IE debugger says that it expects an object and brakes on "handleArrowKeys(event)".

This works in FF and Chrome

I don't know why this fails, but it does.

A: 

Change

<body onkeydown="handleArrowKeys(event)">

To

<body onkeydown="processArrowKeys(event)">

Not sure how it works in firefox and chrome since there is no handleArrowKeys function.

Joel Potter
that would be a typo in the question...
Jcubed
A: 

Try the following:

/* ... */
function processArrowKeys(E) {
    if (!E) E = window.event;
/* ... */
Blixt
+1  A: 

This should work out:

 onload = function() {
    var body = document.body,
    personX = 18,
    personY = 13;

    body.onkeydown = function( E ) {
    E = E || window.event;

    if (E.keyCode == 37 || E.keyCode == 38 || E.keyCode == 39 || E.keyCode==40) {
        if ( E.preventDefault ) {
     E.preventDefault();
        } else {
     E.returnValue = false;
        }
    }

    if (E.keyCode == 37) {
        if (currentterrain[personX - 1][personY] == 0 || currentterrain[personX - 1][personY] == 1 || currentterrain[personX - 1][personY] == 3) {
     personX--;
        }
    }
    if (E.keyCode == 39) {
        if (currentterrain[personX + 1][personY] == 0 || currentterrain[personX + 1][personY] == 1 || currentterrain[personX + 1][personY] == 3) {
     personX++;
        }
    }
    if (E.keyCode == 38) {
        for (i = 0; i < 3; i++) {
     if (currentterrain[personX][personY - 1] == 0 || currentterrain[personX][personY - 1] == 1 || currentterrain[personX][personY - 1] == 3) {
         personY--;
     }
        }
    }

    }
}
meder
where do i put that in the code?
Jcubed
Place this inside the <script> anywhere. The main issue is that you're trying to invoke preventDefault which doesn't exist in IE, event.returnValue=false is the alternative.
meder
+2  A: 

changing this line fixed it:

if(E.keyCode==37||E.keyCode==38||E.keyCode==39||E.keyCode==40){if(navigator.appName!="Microsoft Internet Explorer"){E.preventDefault();}}

IE must not work with preventDefault()

Jcubed
That's what I basically did in my reworked code example. IE supports returnValue = false.
meder