【问题标题】:Collision Checking With JavaScript使用 JavaScript 进行冲突检查
【发布时间】:2019-07-03 07:18:36
【问题描述】:

我正在创建一个游戏,用户在墓地周围徘徊并从不同的坟墓中收集故事。这是一款经典的自上而下的游戏。我正在构建一个脚本,如果用户走进坟墓,他们的运动就会停止,但我在设置碰撞时遇到了麻烦。我正在使用 jQuery。这是我目前所拥有的:

var position = -1;
var $char = $('#char');
var keyCode = null;
var fired = false;
var $stones = $('.stones div');
var collision = null;

document.onkeydown = function(e) {

keyCode = e.which || e.keyCode;

if (!fired) {
    position = -1;
    fired = true; 
    switch (keyCode) {
        case 38: position = 0; break; //up
        case 40: position = 1; break; //down
        case 37: position = 2; break; //left
        case 39: position = 3; break; //right
    }

    walking();
    stepping = setInterval(walking,125);
}

};

document.onkeyup = function(e) {
  //standing
  clearInterval(stepping);
  stepping = 0;
  fired = false;
};


function walking() {

$stones.each(function() { //check all the stones...

    collision = collision($(this), $char, position); ...for collisions

    if (collision) { //if any, then break loop
        return false; 
    }

});

if (!collision) { //check if there was a collision
   //if no collision, keep walking x direction
}


function collision($el, $charEl, position) {

var $el = $el[0].getBoundingClientRect();
var $charEl = $charEl[0].getBoundingClientRect();

var elBottom = parseInt($el.bottom);
var elRight = parseInt($el.right);
var elLeft = parseInt($el.left);
var elTop = parseInt($el.top);

var charBottom = parseInt($charEl.bottom);
var charRight = parseInt($charEl.right);
var charLeft = parseInt($charEl.left);
var charTop = parseInt($charEl.top);

//this is where I'm stuck

}
}

我尝试了各种不同的代码,但似乎没有任何效果。我一直有一个问题,如果我向前走,然后撞到墓碑,然后转身,我就会被卡住。这是我的意思的示例代码:

if (position == 0 && 
    !(elTop > charBottom ||
    elBottom < charTop ||
    elRight < charLeft + 1 ||
    elLeft > charRight - 1)
   ) {
    return true; 
}


if (position == 1 && 
    !(elTop > charBottom ||
    elBottom < charTop ||
    elRight < charLeft + 1 ||
    elLeft > charRight - 1)
   ) {
    return true; 
}

return false;

我看过this questionthis questionthis question 到目前为止我没有任何运气。有人可以帮助我了解逻辑或提供我需要做什么的示例代码吗?

谢谢。

【问题讨论】:

    标签: javascript jquery collision-detection collision boundary


    【解决方案1】:

    你的游戏看起来不错!

    我最近写了一些碰撞检测并且遇到了完全相同的问题。问题是,一旦您的坐标在碰撞情况下为真,那么它们在任何其他运动中都将始终为真。

    您需要存储角色之前所处的位置并恢复到该位置,或者在更改角色坐标之前执行检查。

    【讨论】:

      【解决方案2】:

      我设法找到了以下解决方案,这要感谢 stwitz 的想法以及这个脚本:https://magently.com/blog/detecting-a-jquery-collision-part-iv/

      var position = -1;
      var $char = $('#char');
      var keyCode = null;
      var fired = false;
      var stepSize = 32;
      var $stones = $('.stones div');
      
      //new
      var cancelTop = cancelRight = cancelLeft = cancelBottom = false;
      
      var charEl = $char[0].getBoundingClientRect();
      var charLeft = parseInt(charEl.left);
      var charRight = parseInt(charEl.right);
      var charTop = parseInt(charEl.top);
      var charBottom = parseInt(charEl.bottom);
      
      function walking() {
      
      if (position == 0 && !cancelTop) {
          //if moving up & is safe to move up
      } else if (position == 1 && !cancelBottom) {
          //if moving down & is safe to move down
      } else if (position == 2 && !cancelLeft) {
         //if moving left and is safe to move left
      } else if (position == 3 && !cancelRight) {
         //if moving right and is safe to move right
      }
      
      cancelTop = cancelRight = cancelLeft = cancelBottom = false; //mark all as safe until we check
      
      $stones.each(function() {
      
          collision($(this));
      
      });
      
      }
      
      document.onkeydown = function(e) {
      
      keyCode = e.which || e.keyCode;
      
      if (!fired) {
          position = -1;
          fired = true; 
          switch (keyCode) {
              case 38: position = 0; break; //up
              case 40: position = 1; break; //down
              case 37: position = 2; break; //left
              case 39: position = 3; break; //right
          }
      
          walking();
          stepping = setInterval(walking,125);
      }
      
      };
      
      document.onkeyup = function(e) {
        //standing
        clearInterval(stepping);
        stepping = 0;
        fired = false;
      };
      
      
      function collision($el) {
      
      var el = $el[0].getBoundingClientRect();
      
      var elBottom = parseInt(el.bottom);
      var elRight = parseInt(el.right);
      var elLeft = parseInt(el.left);
      var elTop = parseInt(el.top);
      
      if ( 
          (elRight == charLeft) &&
          (elBottom - stepSize >= charBottom && charBottom >= elTop + stepSize)
          ) { 
          cancelLeft = true;
          return true;  
      }
      
      if ( 
          (elLeft == charRight) &&
          (elBottom - stepSize >= charBottom && charBottom >= elTop + stepSize)
          ) { 
          cancelRight = true;
          return true;  
      }
      
      if ( 
          (elTop + stepSize > charBottom) && 
          (elTop <= charBottom) && 
          (elLeft < charRight) && 
          (elRight > charLeft) 
          ) 
      { 
          cancelBottom = true;
          return true; 
      }
      
      if ( 
          (elBottom - stepSize < charTop) && 
          (elBottom >= charTop) && 
          (elLeft < charRight) && 
          (elRight > charLeft) 
          ) 
      { 
          cancelTop = true;
          return true; 
      }
      
      return false;
      }
      

      【讨论】:

        猜你喜欢
        • 2016-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多