简体   繁体   English

A-Frame组件之间的异步交互

[英]Async interactions between A-Frame components

I'm currently trying to spawn an object in case there's an intersection between a laser object and a collidable object. 我目前正在尝试生成一个对象,以防激光对象和可碰撞对象之间存在交叉点。 I'm using a raycaster to detect the collision. 我正在使用光线投射器来检测碰撞。

To spawn the object I want to do it only in the case there's a collision and also the user has pressed the trigger button. 要生成对象,我只想在发生碰撞并且用户已按下触发按钮的情况下进行操作。

I was thinking on creating a global variable pressed when the triggerdown event listener was pressed and in the raycaster-intersection event listener spawn an object only if this variable was set to true. 我想创建一个全局变量pressedtriggerdown按下事件侦听器,并在raycaster-intersection事件侦听器产卵仅当此变量设置为true的对象。

const pressed = false

AFRAME.registerComponent('laser', {
  init: function () {
    const laser = this.el.sceneEl.querySelector('#laser');
    laser.addEventListener('raycaster-intersection', function(event) {
      console.log('raycaster-intersection', event.detail.intersections[0].point)
      if (pressed) {
        console.log('spawn')
      }
    });
  }
})

AFRAME.registerComponent('spawner', {
  init: function () {
    const blockHand = this.el.sceneEl.querySelector('#blockHand');
    blockHand.addEventListener('triggerdown', function(event) {
      pressed = true
    });
    blockHand.addEventListener('triggerup', function(event) {
      pressed = false
    });
  }
})

I don't like to use global variables but I don't know how could I tackle this problem without them in this case. 我不喜欢使用全局变量,但在这种情况下,我不知道如何在没有它们的情况下解决该问题。

Any suggestions? 有什么建议么?

Thanks! 谢谢!

Some solutions: 一些解决方案:

  1. Do this.el.addState('pressed') and this.el.removeState('pressed') to update the state, and use this.el.is('pressed') to check it. 执行this.el.addState('pressed')this.el.removeState('pressed')以更新状态,并使用this.el.is('pressed')进行检查。

  2. Combine the components together and store it as this.pressed . 将这些组件组合在一起并按this.pressed存储。

  3. A-Frame master build, soon 0.6.0, has a laser-controls component you can use https://aframe.io/docs/master/components/laser-controls.html so all you have to do is listen for click rather than having to listen for both raycaster intersection and trigger down. 即将于0.6.0发行的A-Frame主版本具有一个激光控件组件,您可以使用https://aframe.io/docs/master/components/laser-controls.html,因此您要做的就是听click而不是而不是必须同时收听raycaster交集并向下触发。 And you get all controller support for free. 您将免费获得所有控制器支持。

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

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