JavaScript KeyDown冲突

时间:2012-10-01 13:26:24

标签: javascript

我不知道如何解决问题: 当我按住左侧+空格时,动画可以正常运行,但是当我按按下时(不留下它)然后按空格(有一次,保持按下左侧),动画跳跃但不会向左移动......即使我按下其他键(重置它我必须全部保留),动画也会停止。有人可以解释我为什么会这样,并给我一些实际的解决方案?非常感谢你!这是代码:

<style>
#map{
    position:relative;
    height:220px;
    width:800px;
}
#character{
    height:100px;
    width:100px;
    position:absolute;
    bottom: 10px;
    left: 0px;
    z-index:1;
}
</style>
<div id="map"><div id="character"><img src="images/bart.gif"></div></div>
<script>
enableJump = true;
jump = function(){
    if(enableJump){ enableJump = false;
        salta = function(){
            if(flagY){
                posY++;
                if(posY >= 90) flagY = false;
                jj = document.getElementById('character').style.bottom=posY;
            }
            else{
                posY--;
                jj = document.getElementById('character').style.bottom=posY;
                if(posY < 10){
                    flagY = true;
                    clearInterval(j);
                    enableJump = true;
                    up = false;
                }
            }
        }
        j = setInterval(salta,8);
    }
}
up = false; dx = false;
document.onkeydown = function(evt){
    code = evt.keyCode;
    if(code == 32) up=true;
    if(code == 39) dx=true;
    moveCharacter();
}
document.onkeyup = function(evt){
    code = evt.keyCode;
    if(code == 32) up=false;
    if(code == 39) dx=false;
}
posX = 10; posY = 10; flagY = true; var j;
moveCharacter = function(){
    if(dx == true && up == true){
        jump();
        posX+=10;
        x1 = document.getElementById('character').style.backgroundImage='url(images/character_left.gif)';
        temp = document.getElementById('character').style.left=posX;
    }
    else if(dx){
        posX+=10;
        x1 = document.getElementById('character').style.backgroundImage='url(images/character_left.gif)';
        temp = document.getElementById('character').style.left=posX;
    }
    else if(up){ jump(); }
}
</script>

代码已清理(没有跳转,但遇到同样的问题,按住左键并按下另一个键停止,我必须让它重置它)

<script>
var dx = false;
var posX = 10;
var posY = 10;
document.onkeydown = function(evt){
    if(evt.keyCode == 39) dx=true;
    moveCharacter();
}
document.onkeyup = function(evt){
    if(evt.keyCode == 39) dx=false;
}
moveCharacter = function(){
    if(dx){
        posX+=10;
        document.getElementById('character').style.left=posX;
    }
}
</script>

3 个答案:

答案 0 :(得分:0)

JavaScript只会在最后一个按键上重复keydown个事件。您需要存储正在按下的键的状态,同时收听keydownkeyup事件。

答案 1 :(得分:0)

这可能无法解答您的问题,但会帮助您更好地设计游戏逻辑:http://jsfiddle.net/N2UYx/

答案 2 :(得分:0)

最后我解决了这个问题: 功能keydown,keyup,movecharacter已删除。 我补充说:

var keysDown = {};
try {
    if (window.addEventListener) {
        window.addEventListener("keydown", function (v) {keysDown[v.keyCode] = true;}, false);
        window.addEventListener("keyup", function (v) {delete keysDown[v.keyCode];}, false);
    } else if (document.attachEvent) {
        document.attachEvent("onkeydown", function (v) {keysDown[v.keyCode] = true;});
        document.attachEvent("onkeyup", function (v) {delete keysDown[v.keyCode];});
    } else if (window.attachEvent) {
        window.attachEvent("onkeydown", function (v) {keysDown[v.keyCode] = true;});
        window.attachEvent("onkeyup", function (v) {delete keysDown[v.keyCode];});
    } else {
        document.addEventListener("keydown", function (v) {keysDown[v.keyCode] = true;}, false);
        document.addEventListener("keyup", function (v) {delete keysDown[v.keyCode];}, false);
    }
} catch (e) {
    alert("Keys don't work!\nError: "+e);
}
...
var update = function () {
    if (38 in keysDown) { // Player holding up
        ..
    }
    if (40 in keysDown) { // Player holding down
        ..
    }
    if (37 in keysDown) { // Player holding left
        ..
    }
    if (39 in keysDown) { // Player holding right
        ..
    }
};
setInterval(update,10);

参考:http://www.lostdecadegames.com/how-to-make-a-simple-html5-canvas-game/ http://stackoverflow.com/questions/9507200/why-keydown-listener-doesnt-work-in-ie

适用于Chrome,Firefox,IE7 / 8 / Quirks模式(不是文档模式IE9标准) 感谢您的关注!