p5.js 声音:如何使用 removeCue() 删除单个提示

p5.js sound: How to remove individual cues with removeCue()

p5.js 声音库文档说 removeCue() 可用于取消提示事件。它说它需要一个 ID 输入,该 ID 输入是 return 从 addCue() 编辑的。

当我调用 addCue 并将结果存储到一个变量时,它没有 return ID。它 return 是 NaN。

下图是我使用p5.js代码编辑器编写的代码示例。

如何获取 ID?

好的,我找到问题了。

这是图书馆的问题https://github.com/processing/p5.js/blob/master/lib/addons/p5.sound.js

看看这个linkhttps://github.com/processing/p5.js/blob/master/lib/addons/p5.sound.js#L2178

它使用 var id = this._cueIDCounter++; 但从未定义 _cueIDCounter

所以我尝试为您的代码定义如下:

Object.defineProperty(mySound,'_cueIDCounter',{value:1,writable:true});

现在它返回了 id。

所以我尝试用 removeCue 删除提示,但令我惊讶的是还有一个问题是错误 Uncaught TypeError: Cannot read property 'splice' of undefined

所以我再次查看了库代码,我意识到在 removeCue 函数中的下一行 https://github.com/processing/p5.js/blob/master/lib/addons/p5.sound.js#L2198 中存在错误,当前代码是

p5.SoundFile.prototype.removeCue = function (id) {
    var cueLength = this._cues.length;
    for (var i = 0; i < cueLength; i++) {
      var cue = this._cues[i];
      if (cue.id === id) {
        this.cues.splice(i, 1);
      }
    }
    if (this._cues.length === 0) {
    }
};

但它应该使用 this._cues.splice(i, 1); 而不是 this.cues.splice(i, 1);