javascript - 在游戏 JavaScript 中添加暂停键

标签 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;
            }};

最佳答案

尝试检查 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.
}

关于javascript - 在游戏 JavaScript 中添加暂停键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22931734/

相关文章:

javascript - 如何允许使用 cookie 注册多个帐户?提供来源

javascript - Mocha Chai - 确认 Body 中有一个 'name' 属性

javascript - AJAX 状态代码 < 400 是否成功?

javascript - 如何使用 Debounce Lodash React Native 加快搜索速度

Javascript filter() 函数不起作用

Javascript:最佳单例模式

javascript - 在 Node 脚本中,如何监视文件的更改并获取更新的内容?

javascript - 登录后如何在 React Navigation 中重定向

javascript - svg 路径元素中的定向渐变

javascript - 在另一个 HTML 文件中调用函数