2012-11-28 9 views
-1

下面我有一個叫做的函數buttonClickResult當用戶單擊圖像時啓動。我希望點擊/關閉可以在兩個值之間重新分配名爲distortionSwitch的變量。一個叫做emptyCable另一個叫做過載。這個變量在函數裏面bufferFunctionName使用按鈕功能切換WebAudio API節點分配時遇到問題

我不太清楚如何將這個變量賦值傳遞給其他函數。

要當用戶點擊該按鈕的變量中bufferFunctionName稱爲distortionSwitch應變量賦值之間切換稱爲emptyCable和一個叫超速

謝謝總結。

var humSnd = new Audio('audio/hum.wav'); 
var button = document.getElementById('btn1'); 

buttonClickResult = function() { 
    function playClickSound(filename) { 
    var snd = new Audio(filename); 
    snd.play(); 
    } 


    button.onmousedown = function buttonClicked() { 

    if (button.className == "off") { 
     button.className = "on"; 
     button.src = 'imgs/on.png'; 
     playClickSound('audio/click.wav'); 
     humSnd.play(); 
     humSnd.loop = true; 


    } else if (button.className == "on") { 
     button.className = "off"; 
     button.src = 'imgs/off.png'; 
     playClickSound('audio/click.wav'); 
     humSnd.pause(); 


    } 

    } 
}; 

buttonClickResult(); 






var context = new webkitAudioContext(); 


// Start Of Web Audio API Buffer & Playback Abstraction 

function audioApiKey(domNode, fileDirectory, bufferFunctionName, incomingBuffer, savedBuffer, xhr) { 

    this.domNode = domNode; 
    this.fileDirectory = fileDirectory; 
    this.bufferFunctionName = bufferFunctionName; 
    this.incomingBuffer = incomingBuffer; 
    this.savedBuffer = savedBuffer; 
    this.xhr = xhr; 


    bufferFunctionName = function() { 
    var distortionSwitch = overdrive; // THIS SHOULD TOGGLE 

    var source = context.createBufferSource(); 
    source.buffer = savedBuffer; 
    source.connect(distortionSwitch.input); 

    distortionSwitch.connect(delay.input); 

    delay.connect(convolver.input); 
    convolver.connect(context.destination); 
    source.noteOn(0); // Play sound immediately 
    }; 

當我下面寫bufferFunctionName代碼,即使它的方式真實範圍的,它應該不能訪問distortionSwitch(我想?)

var distortionSwitch = overdrive;  

bufferFunctionName = function() { 
    var source = context.createBufferSource(); 
    source.buffer = savedBuffer; 
    source.connect(distortionSwitch.input); 

    distortionSwitch.connect(delay.input); 

    delay.connect(convolver.input); 
    convolver.connect(context.destination); 
    source.noteOn(0); // Play sound immediately 
}; 

當我這樣寫它可以正常工作

bufferFunctionName = function() { 

    var distortionSwitch = overdrive; 

    var source = context.createBufferSource(); 
    source.buffer = savedBuffer; 
    source.connect(distortionSwitch.input); 

    distortionSwitch.connect(delay.input); 

    delay.connect(convolver.input); 
    convolver.connect(context.destination); 
    source.noteOn(0); // Play sound immediately 
}; 
+0

你的代碼很混亂,因爲你的縮進是非常不一致的。 – Pointy

+0

對不起,我把它清理乾淨了。請刪除downvote – William

回答

0

如果我明白你的問題吧,我想你想要做的是將這一行:

var distortionSwitch = overdrive; 

,並把它頂配

var humSnd = new Audio('audio/hum.wav'); 
var button = document.getElementById('btn1'); 
var distortionSwitch = overdrive; 

這使得按鈕中的可用distortionSwitch點擊監聽器。然後你做

if (button.className == "off") { 
    button.className = "on"; 
    button.src = 'imgs/on.png'; 
    distortionSwitch = overdrive; 
    playClickSound('audio/click.wav'); 
    humSnd.play(); 
    humSnd.loop = true; 
} else if (button.className == "on") { 
    button.className = "off";  
    button.src = 'imgs/off.png'; 
    distortionSwitch = emptyCable; 
    playClickSound('audio/click.wav'); 
    humSnd.pause(); 
} 
+0

你寫的是有道理的,我試了一下,但是在範圍界定方面有些奇怪。我試圖通過在我的問題中添加附錄來澄清。 – William

+0

嗯。看起來它應該對我有用。你會得到什麼樣的錯誤?我可能需要查看上下文中的代碼以進一步幫助您。 @Taoist –

+0

謝謝,我實際上找到了一個解決方案,它需要移動if語句。我不想讓你知道細節,但爲什麼我必須按照自己的方式修復它,我仍然很困惑。無論如何。永遠感激。 – William