在游戏javascript中添加暂停键

时间:2014-04-08 08:35:28

标签: javascript

我创建了一个小游戏但是我在设置暂停键时遇到了一些麻烦。它不起作用。你能看出什么是错的并纠正我吗?谢谢。我想知道如何实现一个暂停键和一个重启键,以便不刷新页面。

Link of the game 代码:

//this is where the keybinding occurs
$(document).keydown(function(e){
    if(!gameOver && !playerHit){
    if (!gamePaused) {
        game = clearTimeout(game);
        gamePaused = true;
        } else if (gamePaused) {
            game = setTimeout(gameLoop, 1000 / 30);
            gamePaused = false;
            }

按键:

        switch(e.keyCode){
            case 75: //this is shoot (k)
                //shoot missile here
                var playerposx = $("#player").x();
                var playerposy = $("#player").y();
                var name = "playerMissle_"+Math.ceil(Math.random()*1000);
                $("#playerMissileLayer").addSprite(name,{animation: missile["player"], posx: playerposx + 90, posy: playerposy + 14, width: 36,height: 10});
                $("#"+name).addClass("playerMissiles")
                break;
            case 65: //this is left! (a)
                $("#playerBooster").setAnimation();
                break;
            case 87: //this is up! (w)
                $("#playerBoostUp").setAnimation(playerAnimation["up"]);
                break;
            case 68: //this is right (d)
                $("#playerBooster").setAnimation(playerAnimation["booster"]);
                break;
            case 83: //this is down! (s)
                $("#playerBoostDown").setAnimation(playerAnimation["down"]);
                break;

暂停键P:

            case 80: //pause (p)
                pauseGame();
                alert ("paused")
        }

    }
});

Key发布:

//this is where the keybinding occurs
$(document).keyup(function(e){
    if(!gameOver && !playerHit){
    if (!gamePaused) {
        game = clearTimeout(game);
        gamePaused = true;
        } else if (gamePaused) {
            game = setTimeout(gameLoop, 1000 / 30);
            gamePaused = false;
        switch(e.keyCode){
            case 65: //this is left! (a)
                $("#playerBooster").setAnimation(playerAnimation["boost"]);
                break;
            case 87: //this is up! (w)
                $("#playerBoostUp").setAnimation();
                break;
            case 68: //this is right (d)
                $("#playerBooster").setAnimation(playerAnimation["boost"]);
                break;
            case 83: //this is down! (s)
                $("#playerBoostDown").setAnimation();
                break;
            case 80: //pause (p)
                pauseGame();


        }
    }
});

暂停功能:

function Pause () {
if (!gamePaused) {
        game = clearTimeout(game);
        gamePaused = true;
        } else if (gamePaused) {
            game = setTimeout(gameLoop, 1000 / 30);
            gamePaused = false;
            }};

1 个答案:

答案 0 :(得分:2)

尝试并检查P键是否已注册(使用警报或控制台消息)。 然后

我还建议如下:不要在keyUp / keyDown处理程序中设置实际的游戏循环间隔(或调用游戏函数)。将实际游戏代码放在自己的函数中,例如:

bGamePaused = false;
// key handlers
$(document).keydown(function(e){
    switch(e.keyCode){
    case 80: 
      // check if the key is registered
      console.log("p is pressed, pause the game!");
      // toggle the paused status: 
      bGamePaused = !bGamePaused;
      // tell gameQuery to pause or resume the game
      (bGamePaused) ? pauseGame() : resumeGame();
      break;
}
$(document).keyup(function(e){
    switch(e.keyCode){
    // do not check for pause here. you only need to check when the key is either pressed or released, or the function will get called twice.
}