具有新创建组件的实体

A-entity with newly created components

我正在尝试将一个新注册的组件与一个在 a 场景中提供行为的组件包括在一起。

首先,我注册了一个组件来改变鼠标悬停时的颜色。然后,我注册一个组件来创建一个新的圈子。

当我在 a 场景中创建实体时,它应该显示悬停行为,但它没有。

<html>
  <head>
    <script src="https://aframe.io/releases/0.9.2/aframe.min.js"></script>
    <script>
// here, I register the component to change color on hover
      AFRAME.registerComponent('change-color-on-hover', {
        schema: {
        color: {default: 'red'}
        },

        init: function () {
          var data = this.data;
          var el = this.el;
          var defaultColor = el.getAttribute('material').color;

          el.addEventListener('mouseenter', function () {
            el.setAttribute('color', data.color);
          });


          el.addEventListener('mouseleave', function () {
            el.setAttribute('color', defaultColor);
          });
        }
    });

// here, I create new circles that should later show the hover-behavior
      AFRAME.registerComponent('newcircle', {
        schema: {},
        multiple: true,
        init: function () {
            var sceneEl = document.querySelector('a-scene');
            var entityEl = document.createElement('a-ring');
            var posit = {x: 1, y: 0.1, z: -1};
            entityEl.setAttribute('position', posit);
            sceneEl.appendChild(entityEl);    
        }
      });

    </script>
  </head>

  <body>
    <a-scene background="color: #000000">
// here, the circle is created and should show the hover-behavior
      <a-entity newcircle change-color-on-hover></a-entity>          
    </a-scene>
  </body>
</html>

圆圈已创建,我希望它在悬停时会改变颜色,但事实并非如此。你知道为什么吗?

非常感谢!

mouseentermouseleave 事件在空 <a-entity> 父级上侦听。

如果您将组件添加到环中,那么它应该会按您希望的那样工作:

// ...
entityEl.setAttribute('position', posit);
entityEl.setAttribute('change-color-on-hover', '')
// ...

this fiddle 中查看。