Search code examples
javascriptjqueryaudiothree.jsaframe

A-frame change sound properties onclick


I have some sound in my scene using A-frame (https://aframe.io). I also have a button button at the top of my screen. What I'm wondering is how can I make it so when a button is clicked, the rolloffFactor of the sound is decreased from 1 to 0. Current code:

  <html>
          <head>
            <script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
    <script>
    function changeRollOff() {
    /*Code to change factor of roll off here*/
    }
    </script>
        </head>
          <body>
            <a-scene>
            
            <button style="position: fixed; z-index: 10000;" onclick="changeRollOff()"
            >Change RollOffactor</button>    
            
              <a-box position="-1 0.5 -3" rotation="0 45 0" color="#4CC3D9"></a-box>
              <a-sphere position="0 1.25 -5" radius="1.25" color="#EF2D5E"></a-sphere>
              <a-cylinder position="1 0.75 -3" radius="0.5" height="1.5" color="#FFC65D"></a-cylinder>
              <a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4"></a-plane>
        <a-entity id="river" geometry="primitive: plane" material="color: blue"
                  position="-10 0 0" sound="src: url(https://cdn.glitch.com/2ecb29ef-7d11-4ac6-b954-e050a39aa59a%2FCHIPTUNE%2C%20BUT%20FUNKY%20Useful.mp3?v=1630627441361); autoplay: true; rolloffFactor: 1;"></a-entity>
        
              <a-sky color="#ECECEC"></a-sky>
            </a-scene>
          </body>

    </html>

Solution

  • The rolloffFactor is a property of the sound attribute. You can change it with element.setAttribute(component, property, new_value):

    document.getElementById("river").setAttribute("sound", "rolloffFactor", 0);