如何使用特定键实现onkeyup功能

问题描述 投票:0回答:1

我正在尝试创建一个基本的平台游戏,并且我正在研究重力部分。我正在使用一些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>

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

javascript html game-physics keycode onkeyup
1个回答
0
投票

您在哪里:

myGamePiece.speedX = 0;
myGamePiece.speedY = 0;

...只需添加一行以将myGamePiece.gravity设置为其默认值。我没有在您的代码中看到您实际设置初始值的位置,但是应该是这样。类似于:

myGamePiece.gravity = 9;
© www.soinside.com 2019 - 2024. All rights reserved.