js碰撞效果怎样实现?js碰撞测试代码详解

Admin 2021-05-22 群英技术资�

       怎样使用js实现碰撞检测?碰撞效果在很多游戏中都会使用到,下面给大家分享一个js实现碰撞的简单测试。当小物体碰撞大物体,大物体颜色改变,效果如下�

       代码�

<!DOCTYPE html>
<html lang="en">
 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  div {
    position: absolute;
    top: 0px;
    right: 0px;
    bottom: 0px;
    left: 0px;
    margin: auto;
    width: 300px;
    height: 300px;
    background-color: green;
  }
  
  span {
    position: absolute;
    top: 0px;
    left: 0px;
    display: block;
    width: 100px;
    height: 100px;
    background-color: rgb(10, 151, 233);
  }
</style>
 
<body>
  <div></div>
  <span></span>
  <script>
    var div = document.getElementsByTagName('div')[0];
    var span = document.getElementsByTagName('span')[0];
    span.onmousedown = function(e) {
      // 事件对象兼容
      e = window.event || e;
      // 添加全局捕获
      if (span.setCapture) {
        span.setCapture();
      }
      // 鼠标按下获取鼠标距离页面左侧和顶部距�
      var x = e.clientX;
      var y = e.clientY;
      // 元素距离页面左侧和顶部距�
      var elex = span.offsetLeft;
      var eley = span.offsetTop;
      // 鼠标距离元素距离 =鼠标距离页面距离 -元素距离页面距离
      var X = x - elex;
      var Y = y - eley;
      document.onmousemove = function(e) {
        // 鼠标移动 获取鼠标距离页面距离
        // 事件对象兼容
        e = window.event || e;
        var movex = e.clientX;
        var movey = e.clientY;
        // 元素的left和top� =鼠标距离页面距离 -鼠标距离元素距离
        var leftx = movex - X;
        var lefty = movey - Y;
        /*----------------------------------------------------------*/
        // 碰撞检�
        // 1.左侧安全距离 =大盒子距离页面左侧距� -小盒子占位宽
        var safeleft = div.offsetLeft - span.offsetWidth;
        // 2.右侧安全距离 大盒子距离页面左侧距� +大盒子占位宽
        var saferight = div.offsetLeft + div.offsetWidth;
        // 3.上侧安全距离 =大盒子距离页面顶部距� -小盒子占位高
        var safetop = div.offsetTop - span.offsetHeight;
        // 4. 下侧安全距离 = 大盒子距离页面顶部距� + 大盒子占位高
        var safebottom = div.offsetTop + div.offsetHeight;
 
        if (leftx < safeleft || leftx > saferight || lefty < safetop || lefty > safebottom) {
          div.style.background = 'green';
        } else {
          div.style.background = 'red';
        }
 
        /*----------------------------------------------------------*/
 
        // 边界�
        // �
        if (leftx <= 0) {
          leftx = 0;
        }
        // �
        if (lefty <= 0) {
          lefty = 0;
        }
        // �
        var rightx = document.documentElement.clientWidth - span.offsetWidth;
        if (leftx >= rightx)
          leftx = rightx;
        // �
        var righty = document.documentElement.clientHeight - span.offsetHeight;
        if (lefty >= righty) {
          lefty = righty;
        }
 
 
        span.style.left = leftx + 'px';
        span.style.top = lefty + 'px';
      }
      document.onmouseup = function() {
 
          document.onmousemove = null;
          if (span.releaseCapture) {
            span.releaseCapture();
          }
 
 
        }
        // 阻止默认事件
      return false;
    }
  </script>
</body>
 
</html>

       以上就是JS实现简单的碰撞测试的代码,感兴趣的朋友可以参考,想要更好的视觉效果,可以参考代码实现,希望本文对大家有帮助�

文本转载自脚本之�

标签� js碰撞检�

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:[email protected]进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容�

猜你喜欢

成为群英会员,开启智能安全云计算之旅

立即注册
专业资深工程师驻�
7X24小时快速响�
一站式无忧技术支�
免费备案服务
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 � 0668-2555555
在线客服
微信公众号
返回顶部
返回顶部 返回顶部