根据浮生梦老师前端课程内容,你的代码问题在于称号检测方式不正确。在996引擎中,获取玩家称号数据应该使用 `SL:GetMetaValue("TITLES", param)`,但你的参数传递方式有问题。
这是修正后的完整代码:
```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"
}
}
-- 称号ID映射表
local titleIDMap = {
["暗金魔气"] = 51635,
["血木魔气"] = 51636,
["黑水魔气"] = 51637,
["啖火魔气"] = 51638,
["戾土魔气"] = 51639
}
-- 称号列表
local titleList = {"暗金魔气", "血木魔气", "黑水魔气", "啖火魔气", "戾土魔气"}
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.upmain()
else
lib:msg("系统未配置!")
GUI:Win_Close(baixiaosheng.parent._parent)
end
end
end)
end
-- 检测玩家是否拥有指定称号
function baixiaosheng.hasTitle(titleName)
-- 获取称号ID
local titleID = titleIDMap[titleName]
if not titleID then
SL:print("未找到称号ID:" .. titleName)
return false
end
-- 正确获取称号数据
local titlesData = SL:GetMetaValue("TITLES", "")
if not titlesData then
return false
end
-- 检查称号ID是否在玩家称号数据中
-- TITLES返回的是玩家所有称号数据,需要遍历查找
local hasTitle = false
if type(titlesData) == "table" then
for _, titleData in pairs(titlesData) do
if titleData and titleData.id == titleID then
hasTitle = true
break
end
end
end
return hasTitle
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 baixiaosheng.hasTitle(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 baixiaosheng.hasTitle(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
-- 更新称号图片
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.updateTitleImages()
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.updateTitleImages()
end, 0.5)
end)
end
end
--注册游戏事件
SL:RegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "baixiaosheng", function(data)
if data and data.opera == 1 then
baixiaosheng.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_SKILL_UPDATE, "baixiaosheng", function(data)
baixiaosheng.upmain()
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")
end
end)
return baixiaosheng
```
**问题分析:**
1. 你原来的 `SL:GetMetaValue("TITLES", titleName)` 第二个参数应该是称号ID或空字符串,而不是称号名称
2. `TITLES` 返回的是玩家所有称号数据表,需要遍历查找特定ID
3. 我添加了 `titleIDMap` 映射表来对应称号名称和ID
**主要修改:**
1. 添加了称号ID映射表 `titleIDMap`
2. 修改了 `hasTitle` 函数,正确获取和遍历称号数据
3. 保持了原有按钮逻辑,没有添加额外按钮
这样就能正确检测玩家是否拥有这些特定ID的称号了。