是否有可能在a帧中获得触摸事件(在移动浏览器上)

Is it possible to get touch events in a-frame (on mobile browser)?

本文关键字:事件 移动 触摸 浏览器 有可能 是否      更新时间:2023-09-26

我已经尝试添加一个onclick事件到我的框架实体,像这样:

<a-sphere id="sphere1" onclick="moveSphere()" position="0 1.25 -1" radius="1.25" color="#EF2D5E"></a-sphere>

但是不能在手机上使用

或者,我试过像这样添加触摸事件监听器,但什么也没发生:

sphereElement.addEventListener('touchend', moveSphere);

3D元素不像DOM元素,你不能像touchend那样在它们上面注册普通的DOM事件。你必须在canvas

上注册它们

要做到这一点,你需要一个像https://jesstelford.github.io/aframe-click-drag-component/

这样的光线投射解决方案。
<head>
  <script src="https://aframe.io/releases/0.3.0/aframe.min.js"></script>
  <script src="https://unpkg.com/aframe-click-drag-component"></script>
  <script>
    registerAframeClickDragComponent(window.AFRAME);
  </script>
</head>
<body>
  <a-scene>
    <a-sphere click-drag position="0 1.25 -5" radius="1.25" color="#EF2D5E"></a-sphere>
    <a-camera look-controls-enabled="false"></a-camera>
  </a-scene>
</body>