<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)">
IE调试器表示它需要一个对象并对“handleArrowKeys(event)”进行制动。
这适用于FF和Chrome
我不知道为什么会失败,但确实如此。
答案 0 :(得分:2)
更改此行修复了它:
if(E.keyCode==37||E.keyCode==38||E.keyCode==39||E.keyCode==40){if(navigator.appName!="Microsoft Internet Explorer"){E.preventDefault();}}
IE不得与preventDefault()
答案 1 :(得分:1)
尝试以下方法:
/* ... */
function processArrowKeys(E) {
if (!E) E = window.event;
/* ... */
答案 2 :(得分:1)
这应该可以解决:
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--;
}
}
}
}
}
答案 3 :(得分:0)
更改
<body onkeydown="handleArrowKeys(event)">
到
<body onkeydown="processArrowKeys(event)">
由于没有handleArrowKeys函数,因此不确定它在firefox和chrome中是如何工作的。