Livecode:两种不同的按钮功能取决于之前是否单击过

Livecode: Two different button functions depending if clicked before

在 Livecode 中,我试图让一个按钮使用 "connect"/"disconnect" 函数。我怎样才能让按钮知道它之前是否已经被点击过,执行代码 A 然后,当再次点击时,应该执行代码 B?在"connected"和"disconnected"

之间需要点击几次按钮

如果您使用的是标准按钮并且只有两种状态 (disconnected/connected),一个简单的方法是禁用按钮的 autoHilite 属性 并手动设置按钮的 hilite它的脚本:

on mouseUp
   set the hilite of me to not the hilite of me
   if the hilite of me then
       -- do connecting stuff here
   else
       -- do disconnecting stuff here
   end if
end mouseUp



您没有解释为什么需要多次单击按钮,但是假设您需要两个以上的连接状态,您可以使用自定义 属性 来存储当前状态。例如,您可以为每个值使用 'empty'(断开连接)、'handshaking'(启动进程)、'connecting'(进程中)和 'linked'(连接)的值状态。类似于:

# STORE CURRENT CONNECTING STATE IN connectionState

on mouseUp
   switch the connectionState of me
      case empty
         -- start connection process, show HANDSHAKING feedback here
         hilite me
         set the connectionState of me to "handshaking"
      break
      case "handshaking"
         -- if initial handshake successful, begin connecting to system
         if handShakeSuccessful is true then
             -- start connection process, show CONNECTING feedback here
             set the connectionState of me to "connecting"
         end if
      break
      case "connecting"
         -- if initial connection is successful, show LINKED feedback here
         if connectionSuccessful is true then
            set the connectionState of me to "linked"
         end if
      break
      case "linked"
         -- do disconnecting stuff here
         set the connectionState of me to empty
         unhilite me
   end switch
end mouseUp

在 ChatRev 堆栈中,我们使用按钮的标签来指示其状态。

if the label of me is "Connect" then
  set the label of me to "Disconnect"
  open socket gSocket
  write "helo" & cr to socket gSocket
  read from socket gSocket with message "connected"
else // "Disconnect"
  set the label of me to "Connect"
  write "disconnect" to socket gSocket
  repeat for each line myLine in the openSockets
    close socket myLine
  end repeat
end if

如果标签为 "Connected",则应关闭连接;如果标签为 "Connect",则应建立连接。

我通常在这种情况下使用 setProp 处理程序。该按钮仅用作 "toggle" 开关。

# the button script
on mouseUp
   set the uConnectedState of this cd to not the uConnectedState of this cd
end mouseUp

# in the card script (or wherever appropriate)
setProp uConnectedState pIsConnected
   if pIsConnected then
      set the label of btn "connect" to "Disconnect"
      # any other interface updates or logic here
   else
      set the label of btn "connect" to "Connect"
      # any other interface updates or logic here
   end if
   pass uConnectedState
end uConnectedState

这样做意味着我所要做的就是设置自定义 属性,所有业务都由 setProp 处理程序处理。例如,如果我需要在 openCard 上初始化连接,我所要做的就是将卡的 uConnectedState 设置为 false/true.