Mozilla AFRAME a-frame 启动检查器从按钮 onclick 而不是击键

Posted

技术标签:

【中文标题】Mozilla AFRAME a-frame 启动检查器从按钮 onclick 而不是击键【英文标题】:Mozilla AFRAME a-frame launch inspector from button onclick instead keystroke 【发布时间】:2017-01-16 10:59:53 【问题描述】:

我们都知道您可以使用 strg+alt+i 启动检查器,但我现在正尝试在我的用户界面中添加一个按钮来执行相同的操作:

function customEnterInspector () 
        var scene = document.querySelector('a-scene');
        if (scene) 
            if (scene.hasLoaded) 
                this.injectInspector();
             else 
                scene.addEventListener('loaded', this.injectInspector());
            
        
    

    $( "#intoinspector" ).click(function() 
        customEnterInspector();
    );

但是,我确实得到了错误:

 Uncaught TypeError: this.injectInspector is not a function
at customEnterInspector (index.html:925)
at HTMLAnchorElement.<anonymous> (index.html:933)
at HTMLAnchorElement.dispatch (jquery-3.1.1.min.js:3)
at HTMLAnchorElement.q.handle (jquery-3.1.1.min.js:3)

我想我引用的元素(场景)不是正确的元素。我已经尝试过使用“窗口”,但这也不起作用。有什么我可以尝试的建议吗?

感谢和最好的, - 最大

【问题讨论】:

关于 JS 语法的旁注:foo.addListener('event', callback()) 将不起作用,因为callback() 被立即调用,而不是传递给侦听器。相反,您需要foo.addListener('event', callback),它只是将函数交给侦听器,以便稍后调用。添加() 使函数立即运行,这是我们不希望的。 More details. 【参考方案1】:

这段代码也很好用。

function openEditor()

    window.postMessage('INJECT_AFRAME_INSPECTOR','*');

我已将此函数附加到执行先前代码的图像:

<input type="image" src="images/edit.png"   onclick="openEditor();"/>

【讨论】:

【参考方案2】:

injectInspector() 方法是场景对象上inspector component 的一部分。在您上面发布的代码 sn-p 中,this 指的是按钮元素,它没有该方法。要引用组件,您可以这样做:

 var inspector = scene.components.inspector;

 // Show inspector immediately.
 inspector.injectInspector();

 // Show inspector after event.
 scene.addEventListener('loaded', function () 
   inspector.injectInspector();
 );

【讨论】:

以上是关于Mozilla AFRAME a-frame 启动检查器从按钮 onclick 而不是击键的主要内容,如果未能解决你的问题,请参考以下文章

A-Frame IOS Inline video Phonegap

A-Frame三星gearVR示例

A-frame 显示鼠标指针和函数

如何在Aframe中向实体添加淡入淡出过渡

360 度图像的 A-Frame 用户滑动效果

如何在 aframe-v1.1.0.min 中使用 collada 模型