0

我正在尝试创建一个函数来创建一个按钮(所以保持“干净”的代码)。

这是代码:

(
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”。为什么?

对不起,我是初学者。

谢谢!

4

1 回答 1

0

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

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

换句话说,如果您尝试评估

variableName

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

顺便说一句,您可以只使用w而不是~w; 默认情况下,单字母变量名是全局的,这是惯用的用法。

-布赖恩

于 2017-10-07T19:39:03.733 回答