简体   繁体   English

我有一个检测2格之间碰撞的功能。 碰撞结果为真时如何执行新功能?

[英]I have a function detecting collision between 2 divs. How do I execute a new function when the collision result is true?

edit: To put it simply, I can detect when frogger gets run over by a car, but I am unsure how to use that result to run the next function to hide frogger. 编辑:简单地说,我可以检测到frogger何时被汽车撞倒,但是我不确定如何使用该结果来运行下一个函数来隐藏frogger。

I am making a simple game of Frogger for a class assignment and so far I have managed to get the collision detection working between 2 divs. 我正在为班级分配制作一个简单的Frogger游戏,到目前为止,我已经设法使碰撞检测在2个div之间起作用。 My question now is, when the collision function is true, how do I use that to run a second function, like hiding the player. 现在我的问题是,当碰撞功能为true时,如何使用它来运行第二个功能,例如隐藏播放器。

function collision($frogger, $car1) {
  var x1 = $frogger.offset().left;
  var y1 = $frogger.offset().top;
  var h1 = $frogger.outerHeight(true);
  var w1 = $frogger.outerWidth(true);
  var b1 = y1 + h1;
  var r1 = x1 + w1;
  var x2 = $car1.offset().left;
  var y2 = $car1.offset().top;
  var h2 = $car1.outerHeight(true);
  var w2 = $car1.outerWidth(true);
  var b2 = y2 + h2;
  var r2 = x2 + w2;

  if (b1 < y2 || y1 > b2 || r1 < x2 || x1 > r2) return false;
  return true;
}
 function handleCollisions() {
    if (collision($('#frogger'), $('#car1'))) {
       $('#frogger').hide; // hide player onCollision
      }
 }

hide是jQuery中的方法而不是属性。

 $('#frogger').hide();

You can use the setInterval () method. 您可以使用setInterval()方法。 enter setInterval(code,millisec) here Whether or not the collision has been detected by the overlap of positions. enter setInterval(code,millisec) here是否已通过位置重叠检测到碰撞。

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

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