简体   繁体   English

使用touchstart事件无法识别碰撞

[英]collision isn't recognized using touchstart event

I am working on creating a touch enabled game using examples from the book "Foundation HTML5 Animation with JavaScript. One of the examples from the book shows basic touch functionality that works but not completely. Here is a link to the example http://jsfiddle.net/yrXCN/3/ 我正在使用“带有JavaScript的HTML5动画基础”一书中的示例来创建具有触摸功能的游戏。该书中的一个示例显示了有效但不完全的基本触摸功能。这是示例http:// jsfiddle的链接.net / yrXCN / 3 /

According to the function below if you touch the canvas within the ball it should log the message "in ball: touchstart" however I have yet to make it work. 根据下面的功能,如果您触摸球内的画布,它应该记录消息“ in ball:touchstart”,但是我还没有使其工作。 It will recognize the ball when you move into it, but for some reason it wont recognize a first touch. 当您进入球时,它会识别出球,但是由于某种原因,它不会识别出第一次触摸。

If I change the code to use mouse listeners it works as intended. 如果我更改代码以使用鼠标侦听器,它将按预期工作。

canvas.addEventListener('touchstart', function (event) {
    event.preventDefault();
    if (utils.containsPoint(ball.getBounds(), touch.x, touch.y)) {
      log.value = "in ball: touchstart";
    } else {
      log.value = "canvas: touchstart";
    }
  }, false);
 element.addEventListener('touchstart', function (event) {
    touch.isPressed = true;
    touch.event = event;
}, false);

If "touchstart" is fired on your ball touch.x and touch.y are undefined and so you do not recognize the touch on the ball. 如果在您的球上触发了“ touchstart”,则touch.xtouch.y未定义,因此您无法识别出球上的触摸。 You track touch.x and touch.y in your touchmove event and that is why you recognize the touch is on the ball while moving. 您在touchmove事件中跟踪touch.xtouch.y ,这就是为什么您在移动时识别出触摸在球上的原因。

Edit: You can solve this by setting touch.x and touch.y inside the touchstart event at line 88-91 编辑:您可以通过在88-91行的touchstart事件中设置touch.xtouch.y来解决此问题

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

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