繁体   English   中英

尝试在AFrame中使用JavaScript将组件添加到实体

[英]Trying to add a component to entity using javascript in AFrame

我正在尝试建立一个库存系统背后的逻辑。 因此,我要尝试的逻辑是有一个检查组件,当单击该组件时,它将基于我告诉组件执行的操作触发一系列事件。 我无法找到通过脚本将组件添加到实体的方法。

我正在尝试阅读addState方法,但这对我的情况没有帮助。 我试过.setAttribute('attribute')也不起作用。 当我查看AFrame检查器时,我要添加的组件未显示在实体下。

<a-scene>
    <a-box  id="box"
        position="0 .5 -3"
        material="color: red"
        pickup="handObj: #handBox; id: handBox"></a-box>

    <a-box  id="followBox"
        position="0 .5 -9"
        visible="false"
        material="color: red"></a-box>

    <a-box  id="interactBox"
        position="3 .5 -3"
        change-color
        logic="item: handBox; event: red"></a-box>

AFRAME.registerComponent('logic', {
    schema: {
        item: {type: 'string', default: ''},
        event: {type: 'string', default: ''}
    },

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

        //var lockedDoor = document.querySlector('#lockedDoor');


        // if(hands[0] == data.item)

        console.log(hands[0]);
        el.addEventListener('click', function() {

            if(hands[0] == data.item) has = true;
            else if (hands[1] == data.item) has = true;

            console.log('hello');
            console.log('hand1 ' + hands[0]);
            console.log('hand2 ' + hands[1]);
            console.log('item ' + data.item);
            console.log(has);

            if(has == true) {
                console.log('hello');
                switch (data.event) {
                    case 'red':
                        document.querySelector('#box').setAttribute('follow');
                        document.querySelector('#box').setAttribute('follow', 'target', '#box');
                        document.querySelector('#box').setAttribute('follow', 'speed', '9');
                        document.querySelector('#followBox').setAttribute('visible', 'true');
                        break;
                }   
            }

        });
    }
});


我只需要找到一种使用脚本将组件添加到实体的方法。

暂无
暂无

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

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