How to load a new scene in A-Frame?

后端 未结 1 1640
时光取名叫无心
时光取名叫无心 2021-01-16 00:13

I am using A-Frame (JavaScript library). I would like to load a new scene when user clicks into a certain component in the current scene. How can I achieve this?

相关标签:
1条回答
  • 2021-01-16 01:05

    Check out the A-Frame Template Component. Notably the Swapping Example.

    You can either define your separate scenes within script tags or within separate files. Here is an example with script tag templating:

    <a-scene>
      <!-- Templates. -->
      <a-assets>
        <script id="scene1" type="text/html">
          <a-box></a-box>
        </script>
        <script id="scene2" type="text/html">
          <a-sphere></a-sphere>
        </script>
      </a-assets>
    
      <a-entity template="src: #box"></a-entity>
    </a-scene>
    

    Then when you want to change your scene, change the src:

    <a-entity template="src: #sphere"></a-entity>
    

    Here is an example component to programmatically change template src on interval: https://github.com/ngokevin/kframe/blob/master/components/template/examples/swapping/components/template-looper.js

    Mainly it will be el.setAttribute('template', 'src', '#sphere');

    For other components that could assist changing the src:

    • Event Set Component can help listen to your mouseenter and change the src in response.
    • The template component also comes with template-set component that will change the template on an event.
    0 讨论(0)
提交回复
热议问题