Button 如何使用脚本创建按钮数组?

Button 如何使用脚本创建按钮数组?,button,livecode,Button,Livecode,我想用脚本创建一个按钮数组。这包括设置大小和位置,并为其指定鼠标指针 mouseUp处理程序应该是 on mouseUp go to card "aName" end mouseUp 名称列表位于文本变量tCardNames中。每行都有一个卡名。下面的脚本执行此任务 on createButtons repeat with i = 1 to the number of lines of tCardNames put line i of field "cardNames

我想用脚本创建一个按钮数组。这包括设置大小和位置,并为其指定鼠标指针

mouseUp处理程序应该是

on mouseUp
    go to card "aName"
end mouseUp

名称列表位于文本变量tCardNames中。每行都有一个卡名。

下面的脚本执行此任务

on createButtons
   repeat with i = 1 to the number of lines of tCardNames
      put line i of field "cardNames" into tName
      createNamedButton i, tName
   end repeat
end createButtons

on createNamedButton n, aName
   create button
   set the label of it to aName
   put "on mouseUp" & return into s
   put  "go to cd "  & quote & aName & quote& return after s
   put "end mouseUp" after s
   set the script of it to s
   put (10 + 30 * (n -1)) into tDistanceFromTop
   set the top of it to tDistanceFromTop
end createNamedButton

该脚本应该可以正常工作,但由于所有按钮基本上都有相同的脚本,因此可以省略处理程序的脚本部分,并为它们指定一个行为。这是一个关于何时使用行为的好例子。行为按钮脚本如下所示:

on mouseUp
  go cd (the label of the target)
end mouseUp
创建该按钮,将其命名为“goCardBehavior”,将其隐藏,然后在原始处理程序中添加此行,而不是编写脚本的部分:

set the behavior of it to the long ID of button "goCardBehavior"

使用行为的一个优点是,当您以后需要更改脚本时,您只需在一个地方进行更改。

这里有一个稍微不同的方法。对于长列表,对每个重复表单比重复表单更有效,但是在这种情况下可能没有明显的差异

on createButtons
  repeat for each line tBtnName in tCardNames
    createNamedButton tBtnName
  end repeat
end createButtons

on createNamedButton pName
  create button pName
  set the script of btn pName to "on mouseUp" & cr & \
    "go cd " & quote & pName & quote & cr & \
    "end mouseUp"
  put the number of btn pName into tNum
  set the top of btn pName to (10 * 30 * (tNum - 1))
end createNamedButton