Попытка добавить компонент к объекту, используя JavaScript в AFrame - PullRequest
0 голосов
/ 02 мая 2019

Я пытаюсь настроить логику системы инвентаризации в рамке. Таким образом, логика, к которой я стремлюсь, заключается в том, что есть компонент проверки, который при нажатии запускает серию событий, основанных на том, что я говорю компоненту делать. Я не могу найти способ добавить компоненты к объекту через сценарии.

Я пытался прочитать о методе 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;
                }   
            }

        });
    }
});


Мне просто нужно найти способ добавить компонент к объекту, используя скрипт.

1 Ответ

0 голосов
/ 03 мая 2019
...