懒动行不行 发表于 2023-7-10 10:16:43

【搬运Wowpedia】事件Events(三)

Script OnEvent
当调用 event时启动。
(self, event, ...)
参数

self
Frame - 注册的组件本身。
event
string - 事件名称,字符串。
...
Variable arguments -可变参数, 事件的有效负载, 如果有。

细节
需要Frame:RegisterEvent()函数。
例子
用 if, elseif, else, end来处理多个事件:

local frame = CreateFrame("Frame")
frame:SetScript("OnEvent", function(__, event, arg1, arg2, arg3, arg4)
      if event == "PLAYER_LOGIN" then
                print("Logging on")
      elseif event == "PLAYER_REGEN_DISABLED" then
                print("Entering combat")
      elseif event == "PLAYER_REGEN_ENABLED" then
                print("Leaving combat")
      elseif event == "UNIT_SPELLCAST_SENT" and arg1 == "player" and arg4 == 1459 then
                print("Casting arcane intellect")
      end
end)
frame:RegisterEvent("PLAYER_LOGIN")
frame:RegisterEvent("PLAYER_REGEN_DISABLED")
frame:RegisterEvent("PLAYER_REGEN_ENABLED")
frame:RegisterEvent("UNIT_SPELLCAST_SENT")

通过查询表来处理多个事件:

local frame = CreateFrame("Frame")

function frame:PLAYER_LOGIN()
      print("Logging on")
end

function frame:PLAYER_REGEN_DISABLED()
      print("Entering combat")
end

function frame:PLAYER_REGEN_ENABLED()
      print("Leaving combat")
end

function frame:UNIT_SPELLCAST_SENT(unitID, __, __, spellID)
      if (unitID == "player" and spellID == 1459) then
                print("Casting arcane intellect")
    end
end

frame:SetScript("OnEvent", function(self, event, ...)
      self(self, ...)
end)

frame:RegisterEvent("PLAYER_LOGIN")
frame:RegisterEvent("PLAYER_REGEN_DISABLED")
frame:RegisterEvent("PLAYER_REGEN_ENABLED")
frame:RegisterEvent("UNIT_SPELLCAST_SENT")





球刀 发表于 2024-4-19 23:25:41

学习学习,谢谢大佬,感谢分享
页: [1]
查看完整版本: 【搬运Wowpedia】事件Events(三)