LUACN论坛

 找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
热搜: YJWOW MagicStone BoL
查看: 182|回复: 1

[综合] 【搬运Wowpedia】事件Events(三)

[复制链接]
发表于 2023-7-10 10:16:43 | 显示全部楼层 |阅读模式
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")






回复

使用道具 举报

发表于 2024-4-19 23:25:41 | 显示全部楼层
学习学习,谢谢大佬,感谢分享
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

小黑屋|手机版|Archiver|LUACN论坛

GMT+8, 2024-5-2 05:19 PM , Processed in 0.032845 second(s), 15 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表