根据浮生梦老师前端课程内容分析,你的代码可能存在几个关键问题:
## 问题分析:
1. **服务端数据匹配问题**:`SL:GetMetaValue("TITLES", titleName)` 需要确保服务端返回的数据格式与你的称号名称完全匹配
2. **称号状态判断逻辑**:服务端可能返回的是数字或字符串,而不是布尔值
3. **事件触发时机**:可能需要手动触发数据刷新
## 修改后的完整代码:
```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 = {"暗金魔气", "血木魔气", "黑水魔气", "啖火魔气", "戾土魔气"}
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)
-- 从服务端获取称号状态
local titleStatus = SL:GetMetaValue("TITLES", titleName)
-- 调试输出,查看实际返回值
SL:print("检测称号[" .. titleName .. "]状态:", titleStatus)
-- 更宽松的判断逻辑
if titleStatus == true or titleStatus == 1 or titleStatus == "1" then
return true
elseif titleStatus == false or titleStatus == 0 or titleStatus == "0" then
return false
elseif type(titleStatus) == "string" and string.len(titleStatus) > 0 then
-- 如果是非空字符串也认为拥有
return true
else
return false
end
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 .. "]已拥有,设置图片:" .. imageInfo.owned)
else
-- 未拥有称号,显示普通图片
GUI:Image_loadTexture(ui[imageInfo.ui], imageInfo.normal)
SL:print("称号[" .. titleName .. "]未拥有,设置图片:" .. imageInfo.normal)
end
else
SL:print("控件不存在或为nil:", imageInfo and imageInfo.ui or "无")
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.refreshTitleData()
SL:print("手动刷新称号数据")
-- 向服务端请求最新称号数据
local msg = {}
msg.Script = baixiaosheng.param1
msg.Action = "refresh_titles"
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, baixiaosheng.param2, 99, 0, jsonstr)
-- 延迟刷新UI
SL:ScheduleOnce(function()
baixiaosheng.updateTitleImages()
end, 0.3)
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
-- 添加刷新按钮
if not GUI:Win_IsNull(ui["Button_refresh"]) then
GUI:addOnClickEvent(ui["Button_refresh"], function()
SL:print("点击刷新按钮")
baixiaosheng.refreshTitleData()
end)
end
-- 首次加载时立即刷新
baixiaosheng.refreshTitleData()
-- 检查是否集齐所有称号
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
-- ... 其他按钮逻辑保持不变
end
-- 注册游戏事件
SL:RegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "baixiaosheng", function(data)
if data and data.opera == 1 then
SL:print("装备变化,刷新称号显示")
baixiaosheng.updateTitleImages()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_SKILL_UPDATE, "baixiaosheng", function(data)
SL:print("技能更新,刷新称号显示")
baixiaosheng.updateTitleImages()
end)
-- 新增:玩家数据更新事件
SL:RegisterLUAEvent(LUA_EVENT_PLAYER_DATA_UPDATE, "baixiaosheng", function(data)
SL:print("玩家数据更新,刷新称号显示")
baixiaosheng.updateTitleImages()
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_PLAYER_DATA_UPDATE, "baixiaosheng")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "baixiaosheng")
end
end)
return baixiaosheng
```
## 关键改进点:
1. **调试输出**:在`hasTitle`函数中添加了详细的调试输出,查看服务端实际返回的值
2. **宽松判断**:支持多种返回值格式(布尔值、数字、字符串)
3. **手动刷新**:添加了`refreshTitleData`函数,可以手动请求服务端数据
4. **更多事件监听**:添加了`LUA_EVENT_PLAYER_DATA_UPDATE`事件监听
5. **首次加载刷新**:在`upmain`中立即执行一次数据刷新
## 排查建议:
1. 查看控制台输出,确认服务端返回的实际数据格式
2. 确保服务端`TITLES`数据与你的称号名称完全一致
3. 尝试使用手动刷新按钮获取最新数据
如果还有问题,请提供控制台输出的调试信息,这样能更准确地定位问题所在。