local baixiaosheng = {}
-- 定义称号列表
local titleList = {
"暗金魔气",
"血木魔气",
"黑水魔气",
"啖火魔气",
"戾土魔气"
}
-- 终极称号
local wuxingTitle = "五行の暗魔体"
-- 检查是否集齐五个称号
function baixiaosheng.checkAllTitles(actor)
for _, title in ipairs(titleList
看了你的前端代码,问题出在称号检测的逻辑上。根据浮生梦老师前端课程内容,获取玩家称号数据需要正确使用接口函数。以下是完整的修正代码:
```lua
local baixiaosheng = {}
-- 定义称号与图片的对应关系
local titleImageMap = {
["暗金魔气"] = {
ui = "Image_17",
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 playerTitles = SL:GetMetaValue("TITLES", "")
-- 检查称号数据是否存在
if not playerTitles or type(playerTitles) ~= "table" then
SL:print("未获取到玩家称号数据")
return false
end
-- 遍历玩家称号列表查找目标称号
local hasTitle = false
for _, titleData in pairs(playerTitles) do
if titleData and titleData.id and tonumber(titleData.id) == titleID then
hasTitle = true
break
end
end
SL:print("检测称号[" .. titleName .. "]: " .. (hasTitle and "已拥有" or "未拥有"))
return hasTitle
end
-- 更新所有称号图片
function baixiaosheng.updateTitleImages()
if not baixiaosheng.parent or not baixiaosheng.parent._ui then
SL:print("UI对象不存在")
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
else
SL:print("未找到UI控件:" .. (imageInfo and imageInfo.ui or "未知"))
end
end
end
-- 检查是否集齐所有称号
function baixiaosheng.checkAllTitles()
local allOwned = true
for _, titleName in ipairs(titleList) do
if not baixiaosheng.hasTitle(titleName) then
allOwned = false
break
end
end
SL:print("检查称号集齐状态:" .. (allOwned and "已集齐" or "未集齐"))
return allOwned
end
function baixiaosheng.upmain()
if not baixiaosheng.parent or not baixiaosheng.parent._ui then
SL:print("无法更新主界面:parent或ui不存在")
return
end
local ui = baixiaosheng.parent._ui
SL:print("开始更新百晓生界面...")
-- 更新称号图片
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_3"]) then
GUI:addOnClickEvent(ui["Button_3"], function()
SL:print("点击提交化魔志按钮")
GUI:delayTouchEnabled(ui["Button_3"], 0.2)
local msg = {}
msg.Script = baixiaosheng.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, baixiaosheng.param2, 2, 0, jsonstr)
end)
end
if not GUI:Win_IsNull(ui["Button_4"]) then
GUI:addOnClickEvent(ui["Button_4"], function()
SL:print("点击使用化魔志抽奖按钮")
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("点击使用元宝按钮")
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
if not GUI:Win_IsNull(ui["Button_6"]) then
GUI:addOnClickEvent(ui["Button_6"], function()
SL:print("点击兑换终极称号按钮")
GUI:delayTouchEnabled(ui["Button_6"], 0.2)
local msg = {}
msg.Script = baixiaosheng.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, baixiaosheng.param2, 5, 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. **hasTitle函数**:正确使用`SL:GetMetaValue("TITLES", "")`获取玩家称号数据
2. **数据验证**:添加了类型检查和错误处理
3. **调试信息**:增加了详细的打印日志,方便排查问题
4. **数据对比**:使用`tonumber()`确保ID对比正确
现在代码应该能正确检测到你的称号了。如果还有问题,检查一下你的称号ID配置是否正确,或者让服务端确认一下TITLES数据是否正常返回。