繁体   English   中英

我的跳跃脚本在做什么错?

[英]What am I doing wrong in my jumping script?

我正在构建2D平台游戏。 而且我还没有编写用于跳跃等所有内容的脚本,但是我有可以向各个方向移动的基本脚本:

document.onkeydown = checkKey;

function checkKey(e) {

    e = e || window.event;

    if (e.keyCode == '38') {
      if (yVel <= 4){
        yVel += speed;
      }else{
        yVel = yVel
      }
      y -= yVel;

    }
    else if (e.keyCode == '40') {
      if (yVel <= 4){
        yVel += speed;
      }else{
        yVel = yVel
      }
      y += yVel;
    }
    else if (e.keyCode == '37') {
      if (xVel <= 4){
        xVel += speed;
      }else{
        xVel = xVel
      }
      x -= xVel;
    }
    else if (e.keyCode == '39') {
      if (xVel <= 4){
        xVel += speed;
      }else{
        xVel = xVel
      }
      x += xVel;
    }

结果应该是脚本,如果需要的话,它可以让您对角移动,但是我所得到的是,例如,当您按下向上键时,它上升了,但是当您按下任何其他按钮时,例如,向右,那么它会停止向上运动,并且只会向右移动。 这里有项目代码: https : //repl.it/@MarkelL/Break-It这是项目本身: https : //break-it--markell.repl.co/

解释将不胜感激。 谢谢!

这是因为onKeyDown被第二次按键覆盖。 您需要为正在监听的上/下键和左/右键(这样它们可以相互覆盖)编写一个事件侦听器-如果您使用的是诸如Phaser之类的f2d框架,则它们已指定keyDown事件您可以绑定。 是一种检测多次按键的方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM