Script OnEvent
当调用 event时启动。
[Lua] 纯文本查看 复制代码 (self, event, ...)
参数
self
Frame - 注册的组件本身。
event
string - 事件名称,字符串。
...
Variable arguments -可变参数, 事件的有效负载, 如果有。
细节
需要Frame:RegisterEvent()函数。
例子
用 if, elseif, else, end来处理多个事件:
[Lua] 纯文本查看 复制代码 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")
通过查询表来处理多个事件:
[Lua] 纯文本查看 复制代码 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[event](self, ...)
end)
frame:RegisterEvent("PLAYER_LOGIN")
frame:RegisterEvent("PLAYER_REGEN_DISABLED")
frame:RegisterEvent("PLAYER_REGEN_ENABLED")
frame:RegisterEvent("UNIT_SPELLCAST_SENT")
|