【问题标题】:How to implement onkeyup function with a specific key如何使用特定键实现 onkeyup 功能
【发布时间】:2020-01-31 20:49:26
【问题描述】:

我正在尝试创建一个基本的平台游戏,并且正在研究重力部分。我正在使用一些 if myGameArea.keys 语句来允许玩家前后移动角色。当按下向上键时,我正在使用加速功能来停止重力,但是当抬起键时我无法让它重新开始。我知道如何实现 onkeyup 函数 (onkeyup = function();) 但我不知道如何将它添加到我的代码中。

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <style>
      canvas {
        border:1px solid #d3d3d3;
        background-color: #f1f1f1;
      }
    </style>
  </head>
  <body onload="startGame()">
    <script>
      var myGamePiece;

      function startGame() {
        myGameArea.start();
        myGamePiece = new component(30, 30, "red", 10, 240);
      }

      var myGameArea = {
        canvas : document.createElement("canvas"),
        start : function() {
        this.canvas.width = 480;
        this.canvas.height = 270;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, 
        document.body.childNodes[0]);
        this.interval = setInterval(updateGameArea, 20);
        window.addEventListener('keydown', function (e) {
        myGameArea.keys = (myGameArea.keys || []);
        myGameArea.keys[e.keyCode] = (e.type == "keydown");
      })
        window.addEventListener('keyup', function (e) {
        myGameArea.keys[e.keyCode] = (e.type == "keyup");
      })
     },
       clear : function(){
       this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
     }
   }

    function component(width, height, color, x, y, type) {
      this.type = type;
      this.width = width;
      this.height = height;
      this.x = x;
      this.y = y;
      this.speedX = 0;
      this.speedY = 0;
      this.gravity = 0.05;
      this.gravitySpeed = 0;
      this.update = function() {
         ctx = myGameArea.context;
         ctx.fillStyle = color;
         ctx.fillRect(this.x, this.y, this.width, this.height);
         }
      this.newPos = function() {
        this.gravitySpeed += this.gravity;
        this.x += this.speedX;
        this.y += this.speedY + this.gravitySpeed;
        this.hitBottom();
      }
      this.hitBottom = function() {
        var rockbottom = myGameArea.canvas.height - this.height;
        if (this.y > rockbottom) {
          this.y = rockbottom;
        }
      }
    }

   function updateGameArea() {
      myGameArea.clear();
      myGamePiece.speedX = 0;
      myGamePiece.speedY = 0;
      myGamePiece.gravity = 0.1;
      if (myGameArea.keys && myGameArea.keys[37]) {myGamePiece.speedX = -2; 
      }
      if (myGameArea.keys && myGameArea.keys[39]) {myGamePiece.speedX = 2; 
      }
      if (myGameArea.keys && myGameArea.keys[38]) {accelerate(-0.2)}
      myGamePiece.newPos();
      myGamePiece.update();
     }

    function accelerate(n) {
      myGamePiece.gravity = n;
    }
  </script>
</body>
</html>

这只是代码的一小部分。如果有人想要全额,请在下面评论告诉我。感谢您的帮助!

【问题讨论】:

  • 你有一个keydown事件来检测按键吗?似乎这也是您想要放置 keyup 事件注册的地方。
  • 谢谢!我没有意识到,但这是代码的单独部分。我会编辑添加这个,我会尝试在那里改变一些东西。
  • 我也有一个keyup部分。我会看看如何使用它。

标签: javascript html game-physics keycode onkeyup


【解决方案1】:

一些问题:

在您的 keyup 事件处理程序中,您有:

myGameArea.keys[e.keyCode] = (e.type == "keyup");

但这会将属性值设置为true。你想要false,那就这样做吧:

myGameArea.keys[e.keyCode] = false;

(顺便说一句,您以前的问题版本之一中没有此错误,您之前有(e.type == "keydown")

您也可以在keydown 处理程序中简单地设置为true(但那里没有错误)。

其次,即使您触底,gravitySpeed 也会不断增加。这将使该值变得难以置信的大,并且按下向上键时的计数器加速不会有任何可见的效果,除非您等待足够长的时间。相反,请在此处重置 `gravitySpeed::

 this.y = rockbottom;
 this.gravitySpeed = 0; // <----

看它运行:

var myGamePiece;

function startGame() {
    myGameArea.start();
    myGamePiece = new component(30, 30, "red", 10, 240);
}

var myGameArea = {
    canvas: document.createElement("canvas"),
    keys: [], // <--- just initialise here
    start: function() {
        window.focus();
        this.canvas.width = 480;
        this.canvas.height = 180;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, 
        document.body.childNodes[0]);
        this.interval = setInterval(updateGameArea, 20);
        window.addEventListener('keydown', function (e) {
            myGameArea.keys[e.keyCode] = true; // <---
        })
        window.addEventListener('keyup', function (e) {
            myGameArea.keys[e.keyCode] = false; // <---
        })
    },
    clear : function(){
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
}

function component(width, height, color, x, y, type) {
    this.type = type;
    this.width = width;
    this.height = height;
    this.x = x;
    this.y = y;
    this.speedX = 0;
    this.speedY = 0;
    this.gravity = 0.05;
    this.gravitySpeed = 0;
    this.update = function() {
        ctx = myGameArea.context;
        ctx.fillStyle = color;
        ctx.fillRect(this.x, this.y, this.width, this.height);
    }
    this.newPos = function() {
        this.gravitySpeed += this.gravity;
        this.x += this.speedX;
        this.y += this.speedY + this.gravitySpeed;
        this.hitBottom();
    }
    this.hitBottom = function() {
        var rockbottom = myGameArea.canvas.height - this.height;
        if (this.y > rockbottom) {
            this.y = rockbottom;
            this.gravitySpeed = 0; // <----
        }
    }
}

function updateGameArea() {
    myGameArea.clear();
    myGamePiece.speedX = 0;
    myGamePiece.speedY = 0;
    myGamePiece.gravity = 0.1;
    if (myGameArea.keys && myGameArea.keys[37]) {myGamePiece.speedX = -2; }
    if (myGameArea.keys && myGameArea.keys[39]) {myGamePiece.speedX = 2; }
    if (myGameArea.keys && myGameArea.keys[38]) {accelerate(-0.2)}
    myGamePiece.newPos();
    myGamePiece.update();
}

function accelerate(n) {
    myGamePiece.gravity = n;
}


window.onload = startGame;
canvas {
    border:1px solid #d3d3d3;
    background-color: #f1f1f1;
}

【讨论】:

  • 对不起,我已经在另一个变量中设置了这个值。我已经尝试过你的建议,但恐怕它不起作用。为了避免进一步的误解,我会将整个代码编辑到我的问题中。不过谢谢!
  • 这就是为什么我们要求提供足够但最少的代码,使我们能够重现您的问题;-)
  • 更新了我的答案,现在很明显你有一个 gravitySpeed 变量,这不是你的问题,但它是问题的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-08
  • 2011-07-29
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多