2013-07-30 23 views
0

编辑*我这样做:如何将此代码应用到我的音乐

if (vol > 0) { 
    vol -= 0.05; 
    document.getElementById("introMusic").volume = vol; 
    } 

我得到这个错误:未捕获的错误:IndexSizeError:DOM异常1个preGameContentObject.js:102 (匿名函数)。它降低了声音,直到它很低,然后继续玩。

我从来没有用javaScript做音频,我很喜欢noob。这个问题几乎令人尴尬。我在这个网站上发现了这个很棒的功能,为我的pong游戏淡出了我的开场曲。

我开始了音乐这个功能:

setUpGame: function() { 
     this.setUpPaddles(); 
     this.setUpBall("left"); 
     preGameContent.getMouseHover(); 
     preGameContent.drawButtons(); 
     preGameContent.getMouseClick(); 
     document.getElementById('introMusic').play(); 
    }, 

那是最好的做法?有用。

我想知道如何将此功能应用到我的代码:

// Initial volume of 0.20 
// Make sure it's a multiple of 0.05 
var vol = 0.20; 
var interval = 200; // 200ms interval 

var fadeout = setInterval(
    function() { 
    // Reduce volume by 0.05 as long as it is above 0 
    // This works as long as you start with a multiple of 0.05! 
    if (vol > 0) { 
     vol -= 0.05; 
     audio.setVolume(vol); 
    } 
    else { 
     // Stop the setInterval when 0 is reached 
     clearInterval(fadeout); 
    } 
    }, interval); 

我知道在哪里可以调用它,因为我有一个“如果玩游戏按钮点击 - 开始游戏”类型的功能作品,我会弹出它。我在哪里可以参考我的“introMusic”元素ID?谢谢!

回答

0

我去这个,而不是因为它有没有错误(这需要稍微重构):

function fadeVolume(volume, callback) 
        { 
         var factor = 0.02, 
          speed = 50; 
         if (volume > factor) 
         { 
          setTimeout(function(){ 
           fadeVolume((document.getElementById("introMusic").volume -= factor), callback);   
          }, speed); 
         } else { 
          (typeof(callback) !== 'function') || callback(); 
         } 
        } 
        fadeVolume(document.getElementById("introMusic").volume, function(){ 
         console.log('fade complete'); 
         document.getElementById("introMusic").pause(); 
         document.getElementById("introMusic").currentTime = 0; 
        }); 

要停止我用.pause();音频和复位当前的时间,因为.stop();没有工作。有谁知道为什么?谢谢。

相关问题