如何检测场景何时加载到A帧中? [英] How to detect when a scene is loaded in A-Frame?

查看:57
本文介绍了如何检测场景何时加载到A帧中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there some event that is triggered when A-Frame is fully loaded? Right now I’ve managed to get my document.querySelector(".a-enter-vr-button") working, but only after placing it inside a setTimeout function, which seems a bit of a makeshift solution. So if anyone has any way of making a js script fire after A-Frame has fully loaded please let me know!

推荐答案

您可以使用 loaded 事件:

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

但是我们建议您使用组件,因此您不必处理事件等待设置的事情:

But we recommend using components so you don't have to handle waiting on events for things to get set up:

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

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

然后使用HTML中的组件:

Then to use the component from HTML:

<a-scene log="Hello, Scene!">
  <a-box log="Hello, Box!"></a-box>
</a-scene>

这篇关于如何检测场景何时加载到A帧中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆