首页 > 解决方案 > 具有新创建组件的 A 实体

问题描述

我正在尝试将一个新注册的组件与一个在 a-scene 中提供行为的组件一起包含在内。

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

当我在 a-scene 中创建实体时,它应该显示悬停行为,但事实并非如此。

<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>

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

非常感谢你!

标签: javascripthtmlaframe

解决方案


和事件在空父对象mouseenter上监听。mouseleave<a-entity>

如果您将组件添加到环中,那么它应该可以按照您的意愿工作:

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

在这个小提琴中检查一下。


推荐阅读