通过JavaScript

时间:2017-03-30 12:55:32

标签: javascript aframe

我开始使用A-Frame VR框架。我有一个简单的盒子,我想制作动画。

这个框会浮动,当看到框时,它会向上扩展,点击它会旋转:

<a-box id="boxID" position="0 2 -5" rotation="0 45 45" scale="2 2 2" src="#boxTexture">
            <a-animation attribute="position" to="0 2.2 -5" direction="alternate" dur="2000" repeat="indefinite"></a-animation>

<!-- These animations will start when the box is looked at. -->
<a-animation attribute="scale" begin="mouseenter" dur="300" to="2.3 2.3 2.3"></a-animation>
<a-animation attribute="scale" begin="mouseleave" dur="300" to="2 2 2"></a-animation>
<a-animation attribute="rotation" begin="click" dur="2000" to="360 405 45"></a-animation>

我想用Javascript创建上面的内容。我从以下开始:

AFRAME.registerComponent('scale-on-interact', {
    schema: {
        to: {default: '2.5 2.5 2.5'}
    }, 

    init: function () {
        var data = this.data;

    }, 

    update: function() {
        var data = this.data;
        console.log(data.color);

        // MOUSE ENTER EVENT
        this.el.addEventListener('mouseenter', function() {
            console.log("enter");
            this.setAttribute('to', data.to);
        });

        // CLICK EVENT
        this.el.addEventListener('click', function() {
            console.log("click");
        });

        // MOUSE LEAVE EVENT
        this.el.addEventListener('mouseleave', function() {
            console.log("leave");
        });
    }
});

我收到了日志,但是例如在鼠标输入时,该框不会向上扩展。此外,我不知道,也无法找到如何创建多个架构,所以我可以创建一个&#39; to&#39;鼠标输入属性,单击鼠标属性。

1 个答案:

答案 0 :(得分:3)

首先,<a-animation>Animation Component都会使用beginstartEvents属性来指定将自动触发动画的实体上的事件。 <a-animation begin="mouseenter"><a-entity animation__1="property: scale; to: 2 2 2; startEvents: click">

其次,有一个event-set component可以做你想要的,设置属性以响应事件。 <a-entity event-set__1="_event: mouseenter; scale: 2 2 2">

第三,在您的组件中,如果您想设置比例,则应setAttribute('scale', {x: 2, y: 2, z: 2}),而不是to。还要确保该组件已附加到您的实体<a-box id="boxID" scale-on-interact>

最后,如果您希望组件能够拥有多个实例,请在组件中设置multiple: true。然后,您可以使用__分隔符设置多个组件:<a-box id="boxID" component__1="foo: 1; bar: 2" component__two="foo: 2; bar: 3">

相关问题