在javascript中模拟as3.0 hitTestobject

在javascript中模拟as3.0 hitTestobject,javascript,jquery,hittest,Javascript,Jquery,Hittest,我试图用javascript编写一个类似于as3.0的命中测试的函数。 我把圆圈放在屏幕上,这个屏幕在div标签中定义,然后我遍历所有圆圈,看看它们的位置是否重叠 html文件中的内容: <div class="balls"> <script> for (i=0;i<10;i++){ document.write("<div id='ball"+i+"' class='ball'></div>"); } &

我试图用javascript编写一个类似于as3.0的命中测试的函数。 我把圆圈放在屏幕上,这个屏幕在div标签中定义,然后我遍历所有圆圈,看看它们的位置是否重叠

html文件中的内容:

<div class="balls">
  <script>
    for (i=0;i<10;i++){
      document.write("<div id='ball"+i+"' class='ball'></div>");
    }
  </script>
</div>
谁能解释一下它为什么会这样做

提前谢谢


更新:我的算法肯定有错误。我现在用20个“球”试了一下,现在每次都断了,我发现了错误。在hitTest中,在重新定位元素后,我只对第二个元素进行测试,而不是再次对所有元素进行测试。现在100%工作的新功能如下:

放置球:

function setBalls(){
  for (i=0;i<amount;i++){
    $("#ball"+i).css('top',parseInt(Math.random()*height)+'px');
    $("#ball"+i).css('left',parseInt(Math.random()*width)+'px'); 
for (j=0;j<i;j++){
      hitTest($("#ball"+i),$("#ball"+j),j);
    }
 }
}
function setBalls(){
对于(i=0;i
function hitTest(object1,object2){
  var left1 = parseInt(object1.css('left'));
  var left2 = parseInt(object2.css('left'));
  var top1 = parseInt(object1.css('top'));
  var top2 = parseInt(object2.css('top'));
  var width1 = parseInt(object1.width());
  var width2 = parseInt(object2.width());
  var height1 = parseInt(object1.height());
  var height2 = parseInt(object2.height());
  var horTest = false;
  var verTest = false;
  if (((left1 >= left2)&&(left1 <= left2 + width2))||((left2 >= left1)&&(left2 <= left1 + width1))){
    horTest = true;
  }
  if (((top1 >= top2)&&(top1 <= top2 + height2))||((top2 >= top1)&&(top2 <= top1 + height1))){
    verTest = true;
  }
  if(horTest&&verTest){
    console.log("hit");
    object1.css('top',parseInt(Math.random()*height)+'px');
    object1.css('left',parseInt(Math.random()*width)+'px');
    hitTest(object1,object2);
  } 
}
.ball{
  width:50px;
  height:50px;
  background:green;
  border-radius:100%;
  position:absolute;
}
.balls{
  width:500px;
  height:320px;
  background:red;
  position:absolute;
  left:10px;
  top:80px;
}
function setBalls(){
  for (i=0;i<amount;i++){
    $("#ball"+i).css('top',parseInt(Math.random()*height)+'px');
    $("#ball"+i).css('left',parseInt(Math.random()*width)+'px'); 
for (j=0;j<i;j++){
      hitTest($("#ball"+i),$("#ball"+j),j);
    }
 }
}
function hitTest(object1,object2,prevBalls){
  var left1 = parseInt(object1.css('left'));
  var left2 = parseInt(object2.css('left'));
  var top1 = parseInt(object1.css('top'));
  var top2 = parseInt(object2.css('top'));
  var width1 = parseInt(object1.width());
  var width2 = parseInt(object2.width());
  var height1 = parseInt(object1.height());
  var height2 = parseInt(object2.height());
  var horTest = false;
  var verTest = false;
  if (((left1 >= left2)&&(left1 <= left2 + width2))||((left2 >= left1)&&(left2 <= left1 + width1))){
    horTest = true;
  }
  if (((top1 >= top2)&&(top1 <= top2 + height2))||((top2 >= top1)&&(top2 <= top1 + height1))){
    verTest = true;
  }
  if(horTest&&verTest){
    console.log("hit");
    object1.css('top',parseInt(Math.random()*height)+'px');
    object1.css('left',parseInt(Math.random()*width)+'px');
    for (j=0;j<prevBalls;j++){
      hitTest(object1,$("#ball"+j),j);
    }
    hitTest(object1,object2);
  } 
}