我启用了vr-mode-ui,我希望在aframe场景中有一个按钮,该按钮可以通过单击进入vr模式。我的问题是我的js给我一个TypeError:document.getElementById(...)为null,我不知道为什么!
<script>
AFRAME.registerComponent('enter', {
init: function () {
}
});
document.getElementById('startbutton').addEventListener("click", (e)=>{
scene.enterVR(true);
});
</script>
<a-scene id="scene" antialias="true"; cursor="rayOrigin: mouse" vr-mode-ui="enabled: true">
<a-assets>
<img id="startscreen" src="start_overlay.png">
</a-assets>
<!-- Environment -->
<a-sky id="environment" radius="9" rotation="0 -90 0"; material="shader: flat; src: #xxx"></a-sky>
<!-- Camera + cursor + Startscreen + Interaction-->
<a-entity look-controls>
<a-entity id="start">
<a-plane id="startbutton" class="link"; height="0.5"; width="5"; position="0 -0.7 -2" rotation="0 0 0" color="#ffbff0">
<a-text align="center" value="START" width="10" color="#e143a1"></a-text>
</a-plane>
</a-entity>
<a-entity id="cam" camera rotation="0 0 0" mouse-cursor>
<a-cursor id="cursor" color="red"
animation__click="property: scale; startEvents: click; from: 0.1 0.1 0.1; to: 1 1 1; dur: 150"
animation__fusing="property: fusing; startEvents: fusing; from: 1 1 1; to: 0.1 0.1 0.1; dur: 1500"
event-set__1="_event: mouseenter; color: white"
event-set__2="_event: mouseleave; color: red"
fuse="true"
raycaster="objects: .link"></a-cursor>
</a-entity>
</a-entity>
</a-scene>
我t know why my button isn
未被js脚本识别!
请有人帮忙!干杯,可以
答案 0 :(得分:0)
在执行代码之前,该按钮可能未在DOM中出现。要么:
1)将代码扔进AFRAME组件,
2)将代码放在HTML正文之后,或
3)将其放在window.onload
回调中:
window.onload = function() {
doSomething();
};
enterVR()
或exitVR()
。确保您使用的scene
引用实际上包含场景元素。