如何检测A-Frame中何时加载场景?

ngo*_*vin 5 aframe

A-Frame完全加载时是否会触发某些事件?现在我已经设法让我的document.querySelector(".a-enter-vr-button")工作,但只有在将它放在一个setTimeout函数之后,这似乎是一个临时解决方案.因此,如果任何人有任何方法在A-Frame完全加载后使js脚本生效,请告诉我!

ngo*_*vin 9

你可以使用这个loaded活动:

document.querySelector('a-scene').addEventListener('loaded', function () {...})

但我们建议您使用组件,这样您就不必处理等待事件设置的事件:

https://aframe.io/docs/0.4.0/guides/using-javascript-and-dom-apis.html#where-to-place-javascript-code-for-a-frame

AFRAME.registerComponent('log', {
  schema: {type: 'string'},
  init: function () {
    var stringToLog = this.data;
    console.log(stringToLog);
  }
});
Run Code Online (Sandbox Code Playgroud)

然后使用HTML中的组件:

<a-scene log="Hello, Scene!">
  <a-box log="Hello, Box!"></a-box>
</a-scene>
Run Code Online (Sandbox Code Playgroud)