根据浮生梦老师前端课程内容,你的代码在检测称号状态时存在问题。主要原因是`SL:GetMetaValue("TITLES", titleName)`这个函数在996引擎中并不存在,无法正确获取服务端的称号状态。
在996引擎中,获取玩家称号状态的正确方法是使用`@gm_checktitle`命令或服务端下发消息。以下是修改后的完整代码:
```lua
local baixiaosheng = {}
-- 定义称号与图片的对应关系
local titleImageMap = {
["暗金魔气"] = {
ui = "Image_17", -- UI控件名称
normal = "res/beixiaosheng/000012.png", -- 未拥有时的图片
owned = "res/beixiaosheng/000011.png" -- 已拥有时的图片
},
["血木魔气"] = {
ui = "Image_18",
normal = "res/beixiaosheng/000012.png",
owned = "res/beixiaosheng/000011.png"
},
["黑水魔气"] = {
ui = "Image_19",
normal = "res/beixiaosheng/000012.png",
owned = "res/beixiaosheng/000011.png"
},
["啖火魔气"] = {
ui = "Image_20",
normal = "res/beixiaosheng/000012.png",
owned = "res/beixiaosheng/000011.png"
},
["戾土魔气"] = {
ui = "Image_21",
normal = "res/beixiaosheng/000012.png",
owned = "res/beixiaosheng/000011.png"
}
}
-- 称号列表
local titleList = {"暗金魔气", "血木魔气", "黑水魔气", "啖火魔气", "戾土魔气"}
-- 存储称号状态
local titleStatus = {}
function baixiaosheng.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
baixiaosheng.parent = lib:openwin(param1, nil, true, param4) or {}
baixiaosheng.param1 = param1
end
if param2 then
baixiaosheng.param2 = param2
end
if param3 then
baixiaosheng.param3 = param3
end
if baixiaosheng.parent.cfg and next(baixiaosheng.parent.cfg) then
-- 先检测所有称号状态
baixiaosheng.checkAllTitlesStatus()
else
lib:msg("系统未配置!")
GUI:Win_Close(baixiaosheng.parent._parent)
end
end
end)
end
-- 检测单个称号状态(通过GM命令)
function baixiaosheng.checkTitleStatus(titleName)
-- 发送GM命令检测称号
local command = "@gm_checktitle " .. baixiaosheng.getPlayerName() .. " " .. titleName
SL:SendGMCommand(command)
-- 注意:这里需要服务端通过消息返回结果
-- 实际项目中需要服务端配合发送称号状态消息
end
-- 获取玩家名(自定义函数)
function baixiaosheng.getPlayerName()
-- 这里需要根据实际情况获取玩家名
-- 可以从登录信息或服务端获取
local loginData = SL:GetMetaValue("LOGIN_DATA", "")
if loginData and loginData.name then
return loginData.name
end
return ""
end
-- 检测所有称号状态
function baixiaosheng.checkAllTitlesStatus()
-- 初始化所有称号状态为false
for _, titleName in ipairs(titleList) do
titleStatus[titleName] = false
end
-- 这里应该通过服务端消息获取称号状态
-- 临时方案:发送GM命令请求称号状态
for _, titleName in ipairs(titleList) do
baixiaosheng.checkTitleStatus(titleName)
end
-- 延迟更新UI,等待服务端响应
SL:ScheduleOnce(function()
baixiaosheng.upmain()
end, 0.5)
end
-- 更新所有称号图片
function baixiaosheng.updateTitleImages()
if not baixiaosheng.parent or not baixiaosheng.parent._ui then
return
end
local ui = baixiaosheng.parent._ui
-- 遍历所有称号,更新图片
for _, titleName in ipairs(titleList) do
local imageInfo = titleImageMap[titleName]
if imageInfo and not GUI:Win_IsNull(ui[imageInfo.ui]) then
if titleStatus[titleName] then
-- 已拥有称号,显示已拥有图片
GUI:Image_loadTexture(ui[imageInfo.ui], imageInfo.owned)
SL:print("称号[" .. titleName .. "]已拥有")
else
-- 未拥有称号,显示普通图片
GUI:Image_loadTexture(ui[imageInfo.ui], imageInfo.normal)
SL:print("称号[" .. titleName .. "]未拥有")
end
end
end
end
-- 检查是否集齐所有称号
function baixiaosheng.checkAllTitles()
for _, titleName in ipairs(titleList) do
if not titleStatus[titleName] then
return false
end
end
return true
end
function baixiaosheng.upmain()
if not baixiaosheng.parent or not baixiaosheng.parent._ui then
return
end
local ui = baixiaosheng.parent._ui
local cfg = baixiaosheng.parent.cfg
-- 更新称号图片
baixiaosheng.updateTitleImages()
-- 检查是否集齐所有称号
if baixiaosheng.checkAllTitles() then
SL:print("玩家已集齐所有魔气称号!")
if not GUI:Win_IsNull(ui["Image_complete"]) then
GUI:setVisible(ui["Image_complete"], true)
end
end
-- 设置升级按钮
if not GUI:Win_IsNull(ui["Button_1"]) then
GUI:addOnClickEvent(ui["Button_1"], function()
SL:print("点击化魔秘闻按钮")
GUI:delayTouchEnabled(ui["Button_1"], 0.2)
local msg = {}
msg.Script = baixiaosheng.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, baixiaosheng.param2, 0, 0, jsonstr)
GUI:setVisible(ui.Image_3, true)
GUI:setVisible(ui.Image_6, false)
end)
end
if not GUI:Win_IsNull(ui["Button_2"]) then
GUI:addOnClickEvent(ui["Button_2"], function()
SL:print("点击五行暗魔按钮")
GUI:delayTouchEnabled(ui["Button_2"], 0.2)
local msg = {}
msg.Script = baixiaosheng.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, baixiaosheng.param2, 1, 0, jsonstr)
GUI:setVisible(ui.Image_3, false)
GUI:setVisible(ui.Image_6, true)
end)
end
if not GUI:Win_IsNull(ui["Button_4"]) then
GUI:addOnClickEvent(ui["Button_4"], function()
SL:print("点击4按钮")
GUI:delayTouchEnabled(ui["Button_4"], 0.2)
local msg = {}
msg.Script = baixiaosheng.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, baixiaosheng.param2, 3, 0, jsonstr)
-- 点击后刷新图片显示
SL:ScheduleOnce(function()
baixiaosheng.checkAllTitlesStatus()
end, 0.5)
end)
end
if not GUI:Win_IsNull(ui["Button_5"]) then
GUI:addOnClickEvent(ui["Button_5"], function()
SL:print("点击5按钮")
GUI:delayTouchEnabled(ui["Button_5"], 0.2)
local msg = {}
msg.Script = baixiaosheng.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, baixiaosheng.param2, 4, 0, jsonstr)
-- 点击后刷新图片显示
SL:ScheduleOnce(function()
baixiaosheng.checkAllTitlesStatus()
end, 0.5)
end)
end
end
-- 注册服务端消息回调(需要服务端配合)
-- 这里假设服务端会发送消息ID为 58004 的称号状态消息
SL:RegisterLUAEvent(58004, "baixiaosheng_title_status", function(data)
if data and data.titles then
-- 更新称号状态
for titleName, hasTitle in pairs(data.titles) do
titleStatus[titleName] = hasTitle
end
-- 刷新UI
baixiaosheng.updateTitleImages()
end
end)
--注册游戏事件
SL:RegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "baixiaosheng", function(data)
if data and data.opera == 1 then
baixiaosheng.checkAllTitlesStatus()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_SKILL_UPDATE, "baixiaosheng", function(data)
baixiaosheng.checkAllTitlesStatus()
end)
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "baixiaosheng", function(CLOSEWINdata)
if CLOSEWINdata and CLOSEWINdata == "baixiaosheng" then
SL:UnRegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "baixiaosheng")
SL:UnRegisterLUAEvent(LUA_EVENT_SKILL_UPDATE, "baixiaosheng")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "baixiaosheng")
SL:UnRegisterLUAEvent(58004, "baixiaosheng_title_status")
end
end)
return baixiaosheng
```
**主要修改点:**
1. **移除了不存在的函数**:`SL:GetMetaValue("TITLES", titleName)` 在996引擎中不存在
2. **使用GM命令检测称号**:通过 `@gm_checktitle` 命令来检测称号状态
3. **添加服务端消息回调**:需要服务端发送称号状态消息(消息ID 58004)
4. **优化了状态管理**:使用 `titleStatus` 表来存储称号状态
5. **改进了刷新逻辑**:在按钮点击后重新检测所有称号状态
**注意**:这个方案需要服务端配合,服务端需要在玩家打开界面时发送称号状态消息,并在玩家获得称号时主动推送更新。如果服务端不配合,前端无法直接获取到准确的称号状态。