用于创建按钮的功能

时间:2017-09-13 08:07:18

标签: supercollider

我试图创建一个创建按钮的功能(所以保持"清洁"代码)。

以下是代码:

(
Window.closeAll;

~w = Window.new(
    name: "Xylophone",
    resizable: true,
    border: true,
    server: s,
    scroll: false);

~w.alwaysOnTop = true; 

/**
 * Function that creates a button.
 */
createButtonFunc = {
    |
        l = 20, t = 20, w = 40, h = 190, // button position
        nameNote = "note", // button name
        freqs // frequency to play
    |

    Button(
        parent: ~w, // the parent view
        bounds: Rect(left: l, top: t, width: w, height: h)
    )
    .states_([[nameNote, Color.black, Color.fromHexString("#FF0000")]])
    .action_({Synth("xyl", [\freqs, freqs])});
}
)


(
SynthDef("xyl", {
    |
        out = 0, // the index of the bus to write out to
        freqs = #[410], // array of filter frequencies
        rings = #[0.8] // array of 60 dB decay times in seconds for the filters 
    |

    ...
)

错误是:错误:变量' createButtonFunc'未定义。 为什么?

抱歉,我是初学者。

谢谢!

1 个答案:

答案 0 :(得分:1)

回答这个问题可能有点晚了,但我希望这可以帮助其他人解决同样的问题。

您收到该错误的原因是因为您在声明之前使用了变量名称。

换句话说,如果你试图评估

variableName

单凭

,您总会收到错误,因为解释器无法将该名称与其知道的任何其他名称相匹配。要解决此问题,您可以使用全局解释器变量(a - z),环境变量(如~createButtonFunc),或在代码中更早地声明var createButtonFunc。请注意,最后一个意味着您在解释该块之后无法访问该变量名称,这可能是也可能不是一件好事。如果您希望以后能够访问它,我认为编写~createButtonFunc最有意义。

顺便说一下,您可以使用w代替~w;单字母变量名称默认为全局变量,这是惯用法。

-Brian