支持多个输入JQuery

时间:2015-07-27 08:34:44

标签: javascript jquery css3

我正在学习如何在html / css3 / javascript / jquery中开发游戏,我必须支持多个键盘输入。

通过阅读一本书,我写了这段代码:

var pingpong = {};

pingpong.pressedKeys = [];

$(function() {
    // set interval to call gameloop every 30 milliseconds
    pingpong.timer = setInterval(gameloop, 30);
    // mark down what key is down and up into an array called
    // "pressedKeys"
    $(document).keydown(function(e) {
        pingpong.pressedKeys[e.which] = true;
    });

    $(document).keyup(function(e) {
        pingpong.pressedKeys[e.which] = false;
    });
});

function gameloop() {
    movePaddles();
}

function movePaddles() {
    // use our custom timer to continuously check if a key is
    // pressed.
    if (pingpong.pressedKeys[KEY.UP]) {// arrow-up
        // move the paddle B up 5 pixels
        var top = parseInt($("#paddleB").css("top"));
        $("#paddleB").css("top", top - 5);
    }
    if (pingpong.pressedKeys[KEY.DOWN]) {// arrow-down
        // move the paddle B down 5 pixels
        var top = parseInt($("#paddleB").css("top"));
        $("#paddleB").css("top", top + 5);
    }
    if (pingpong.pressedKeys[KEY.W]) {// w
        // move the paddle A up 5 pixels
        var top = parseInt($("#paddleA").css("top"));
        $("#paddleA").css("top", top - 5);
    }
    if (pingpong.pressedKeys[KEY.S]) {// s
        // move the paddle A down 5 pixels
        var top = parseInt($("#paddleA").css("top"));
        $("#paddleA").css("top", top + 5);
    }
}

但它不起作用。我的意思是,桨不动!有什么想法吗?

HTML

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title>MyGame</title>
        <style>
            #playground {
                background: #e0ffe0;
                width: 400px;
                height: 200px;
                position: relative;
                overflow: hidden;
            }
            #ball {
                background: #fbb;
                position: absolute;
                width: 20px;
                height: 20px;
                left: 150px;
                top: 100px;
                border-radius: 10px;
            }
            .paddle {
                background: #bbf;
                left: 50px;
                top: 70px;
                position: absolute;
                width: 30px;
                height: 70px;
            }
            #paddleB {
                left: 320px;
            }
        </style>
    </head>
    <body>
        <header>
            <h1>Test</h1>
        </header>
        <div id="game">
            <div id="playground">
                <div id="paddleA" class="paddle"></div>
                <div id="paddleB" class="paddle"></div>
                <div id="ball"></div>
            </div>
        </div>
        <footer>
            This is an example of creating a Game.
        </footer>
        <script src="js/jquery-1.11.3.js"></script>
        <script src="js/html5games.pingpong.js"></script>
    </body>
</html>

MyGoal

使用S(向下)和W(向上)向左移动,向上箭头(向上)和向下(向下)向右移动。如果我按下S向下箭头,左侧拨片必须向上移动,右侧拨片必须向下移动,同时为时。 (事件不能互相阻止)。

我得到什么

键盘输入无效。桨不动。

更新

我忘了将它添加到.js代码中:

var KEY = {
    UP : 38,
    DOWN : 40,
    W : 87,
    S : 83
};

0 个答案:

没有答案