首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(切换)函数未定义

(切换)函数未定义
EN

Stack Overflow用户
提问于 2020-01-12 23:57:31
回答 2查看 396关注 0票数 1

我正在尝试使用一个按钮来切换P5振荡器的音频开关(因为据我所知,WebAudio API内容现在需要在浏览器中进行交互才能播放音频)。

在设置之前,我有:

代码语言:javascript
复制
var button;

在函数设置中,我有:

代码语言:javascript
复制
button = createButton("audio on/off");
button.mousePressed(toggle);
button.position(32, 180);

在函数切换中,我有:

代码语言:javascript
复制
if (!playing()) {
    osc.start();
    //playing = true; } else {
    osc.stop();
    //playing = false; }

它一直给我错误信息

未定义ReferenceError:切换(草图:第45行)“

我为什么一直拿着这个?任何帮助都将不胜感激。

完整的sketch.js代码是:

代码语言:javascript
复制
var button;
var stepX;
var stepY;
let osc; 

function setup() {
  createCanvas(displayWidth, displayHeight);
  noCursor();
  noStroke();
  colorMode(HSB, width, height, 100); 
  reverb = new p5.Reverb();
  delay = new p5.Delay();
  osc = new p5.Oscillator();
  osc.setType('sine');
  reverb.process(osc, 5, 5);
  osc.start();
  // delay.process() accepts 4 parameters:
  // source, delayTime, feedback, filter frequency
  // play with these numbers!!
  delay.process(osc, 0.6, 0.3, 1000);

  // play the noise with an envelope,
  // a series of fades ( time / value pairs )

  var t = 10
  let text = createP("s-p-e-c-t-r-u-m");

  text.position(30, 30);
  text.style("font-family", "monospace");
  text.style("background-color", "#FFFFFF");
  text.style("color", "#F20FD7");
  text.style("font-size", "12pt");
  text.style("padding", "10px");

  let texty = createP("move mouse to control the <br>sine-wave theramin.<br><br>x axis = pitch 30-3000hz<br>y axis = volume quiet-loud)");

  texty.position(32, 80);
  //texty.style("background-color", "#FFFFFF");
  texty.style("font-family", "monospace");
  texty.style("color", "#FFFFFF");
  texty.style("font-size", "10pt");


  button = createButton("audio on/off");
  button.mousePressed(toggle);
  button.position(32, 180);

}

function draw() {
  let pitch = map(mouseX, 0, width, 30, 3000);
  let volume = map(mouseY, 0, height, 1, 0);
  background(200);
  osc.freq(pitch);
  osc.amp(volume);

  stepX = mouseX + 2;
  stepY = mouseY + 2;

  for (var gridY = 0; gridY < height; gridY += stepY) {
    for(var gridX = 0; gridX < width; gridX += stepX) {
      fill(gridX, height - gridY, 100);
      rect(gridX, gridY, stepX, stepY);
    }
  }


function toggle() {
  if (!playing) {
    osc.start();
    playing = true;
  } else {
    osc.stop();
    playing = false;
    }
  }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-13 05:54:54

toggledraw范围内声明。

函数绘制(){ // .函数切换(){ // .}

将它移出draw,顶级解决问题:

代码语言:javascript
复制
function draw() {

// [...]
}

function toggle() {

// [...]
}

参见示例:

代码语言:javascript
复制
var button;
var stepX;
var stepY;
let osc; 

function setup() {
    createCanvas(displayWidth, displayHeight);
    noCursor();
    noStroke();
    colorMode(HSB, width, height, 100); 
    reverb = new p5.Reverb();
    delay = new p5.Delay();
    osc = new p5.Oscillator();
    osc.setType('sine');
    reverb.process(osc, 5, 5);
    osc.start();
    // delay.process() accepts 4 parameters:
    // source, delayTime, feedback, filter frequency
    // play with these numbers!!
    delay.process(osc, 0.6, 0.3, 1000);

    // play the noise with an envelope,
    // a series of fades ( time / value pairs )

    var t = 10
    let text = createP("s-p-e-c-t-r-u-m");

    text.position(30, 30);
    text.style("font-family", "monospace");
    text.style("background-color", "#FFFFFF");
    text.style("color", "#F20FD7");
    text.style("font-size", "12pt");
    text.style("padding", "10px");

    let texty = createP("move mouse to control the <br>sine-wave theramin.<br><br>x axis = pitch 30-3000hz<br>y axis = volume quiet-loud)");

    texty.position(32, 80);
    //texty.style("background-color", "#FFFFFF");
    texty.style("font-family", "monospace");
    texty.style("color", "#FFFFFF");
    texty.style("font-size", "10pt");

    button = createButton("audio on/off");
    button.mousePressed(toggle);
    button.position(32, 180);
}

function draw() {
    let pitch = map(mouseX, 0, width, 30, 3000);
    let volume = map(mouseY, 0, height, 1, 0);
    background(200);
    osc.freq(pitch);
    osc.amp(volume);

    stepX = mouseX + 2;
    stepY = mouseY + 2;

    for (var gridY = 0; gridY < height; gridY += stepY) {
        for(var gridX = 0; gridX < width; gridX += stepX) {
            fill(gridX, height - gridY, 100);
            rect(gridX, gridY, stepX, stepY);
        }
    }
}

function toggle() {
    if (!playing) {
        osc.start();
        playing = true;
    } else {
        osc.stop();
        playing = false;
    }
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.sound.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.js"></script>

票数 0
EN

Stack Overflow用户

发布于 2020-01-13 00:01:01

尝试将您的函数分配给变量。如果不这样做,您的所有函数都将在运行时悬挂到文件的顶部。还要确保在使用toggle之前声明它!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59709176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档