1

我正在使用下面的 js 代码来执行以下操作:

  1. 查看是否存在任何具有 .varname 名称的对象
  2. 如果他们这样做,请删除它们
  3. 使用特定的 .varname 创建新的
  4. 将对象连接在一起

该代码适用于单次迭代,但我想设置一个循环,以便它重复从 1 到并包括全局变量 (g.channelcount) 的值的函数。

欢迎任何帮助!

function bang(){
  g.channelCount = buff.channelcount();//gets buffer channel count

  var deleteWaveform = this.patcher.getnamed("waveform");//delete waveform and fromsymbol objects
  var deleteSymbol = this.patcher.getnamed("symbol");
  this.patcher.remove(deleteWaveform);
  this.patcher.remove(deleteSymbol);

  var waveform = this.patcher.newdefault(96, 756, "waveform~");//creates new waveform and fromsymbol objects and gives them a scripting name
  var symbol = this.patcher.newdefault(186, 698, "fromsymbol");
  waveform.varname = "waveform";
  symbol.varname = "symbol";
  post(g.channelCount);

  var jsBox = this.patcher.getnamed("js_wave");//connects the objects together
  this.patcher.connect(jsBox, 0, waveform, 0);
  this.patcher.connect(jsBox, 1, symbol, 0);
  this.patcher.connect(symbol, 0, waveform, 0);

  outlet(1, "name loop");//loads the loop buffer into the waveform object
  }
}
4

1 回答 1

0

正如@FrostyZombi3 提到的,您可以使用 for 循环执行此操作:

var channelCount = buff.channelcount();

for (var channel = 1; channel <= channelCount; channel++) {
  post(channel, '\n');
  // do the delete/create work using the channel variable
}
于 2020-09-28T14:57:18.590 回答