首页 > 解决方案 > p5.j​​s 声音:如何使用 removeCue() 删除单个提示

问题描述

p5.j​​s 声音库文档说 removeCue() 可用于取消提示事件。它说它需要一个从 addCue() 返回的 ID 输入。

当我调用 addCue 并将结果存储到变量时,它不会返回 ID。它返回 NaN。

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

我如何获得身份证?

在此处输入图像描述

标签: javascriptweb-audio-apip5.js

解决方案


好的,我发现了问题。

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

看看这个链接https://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

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

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);


推荐阅读