下面我有一個叫做的函數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
};
你的代碼很混亂,因爲你的縮進是非常不一致的。 – Pointy
對不起,我把它清理乾淨了。請刪除downvote – William