这是我用于按键的代码:
<script>
document.body.onkeyup = function(e){
if(e.keyCode == 32){
var cmd = "<?php echo $row['HEADING']; ?>";
if(cmd == "N") myMoveUp();
if(cmd == "E") myMoveRight();
if(cmd == "S") myMoveDown();
if(cmd == "W") myMoveLeft();
}
if(e.keyCode == 37){
ChangeHeadingKP("W");
}
if(e.keyCode == 38){
ChangeHeadingKP("N");
}
if(e.keyCode == 39){
ChangeHeadingKP("E");
}
if(e.keyCode == 40){
ChangeHeadingKP("S");
}
}
</script>
任何帮助将不胜感激。
答案 0 :(得分:1)
我不会依赖时间(例如5秒),因为当您更改它们时,您将必须检查代码。
相反,我依赖于通过调用特定方法来切换标志,例如:
// global javascript variable
var flag = true;
function myFunction() {
var imgName = "";
var zIndexVal = 0;
if (flag) {
imgName = "queen.png";
zIndexVal = -1;
} else {
imgName = "queen2.png";
zIndexVal = 0;
}
flag = !flag;
var index = document.getElementById('concier').style.zIndex = zIndexVal;
var foto = document.getElementById('foto').src = imgName;
return index + foto;
}
这两个函数与keymanager.suspend();
// play animation, cutscene or whatever
keymanager.resume();
一样易于定义,如下所示:
keymanager
剩下的就是当按键被停用时按键事件处理程序将失效:
var keymanager = {
"active": true,
"suspend": () => {
keymanager.active = false;
},
"resume": () => {
keymanager.active = true;
}
};