功能来创建按钮
问题描述:
我试图创建一个函数来创建一个按钮(这样保持“干净”的代码)。功能来创建按钮
下面是代码:
(
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
|
...
)
的错误是:ERROR:变量 'createButtonFunc' 没有定义。 为什么?
很抱歉,但我是个初学者。
谢谢!
答
可能有点晚了回答这个问题,但我希望这可以帮助别人同样的问题。
你得到这个错误的原因是因为你使用一个变量名你宣布之前。
换句话说,如果你尝试在自己的评价
variableName
,您总能获得一个错误,因为该解释无法匹配该名称别的就知道。要解决此问题,可以在代码中使用全局解释器变量(a
-z
),环境变量(如~createButtonFunc
)或声明var createButtonFunc
。请注意,最后一个意思是在解释该块之后,您将无法访问该变量名,这可能是也可能不是一件好事。如果您希望稍后能够访问它,我认为编写~createButtonFunc
是最有意义的。
顺便说一句,你可以只使用w
,而不是~w
;单字母变量名默认是全局的,这就是惯用的用法。
-Brian