local switch_map = {}
local MainButton = require("GUILayout\\2_TopLayout\\L_MainButton.lua", true)
function switch_map.main()
local mainWin = MainAssist._ui["Panel_assist"]
local mapID = SL:GetMetaValue("MAP_ID")
-- switch_map.zhuanpan_
根据浮生梦老师前端课程内容,我来帮你检查代码中的问题:
## 主要问题:
1. **定时器管理问题**:
- 在`main()`函数开头有停止定时器的逻辑,但缺少对`switch_map._displayWidgets`的清理
- 定时器回调中没有检查`switch_map._displayWidgets`是否为nil
2. **控件引用问题**:
- `switch_map._displayWidgets`在特殊地图逻辑中创建,但在正常地图逻辑中没有清理
- 定时器回调中直接使用`switch_map._displayWidgets`,可能为nil
3. **网络回调问题**:
- `networkCB`函数中直接调用`switch_map.main()`,没有检查`switch_map`是否已初始化
## 修复后的代码:
```lua
local switch_map = {}
local MainButton = require("GUILayout\\2_TopLayout\\L_MainButton.lua", true)
function switch_map.main()
-- 检查MainAssist是否初始化
if not MainAssist or not MainAssist._ui then
SL:Print("MainAssist未初始化")
return
end
local mainWin = MainAssist._ui["Panel_assist"]
if GUI:Win_IsNull(mainWin) then
SL:Print("mainWin为空")
return
end
local mapID = SL:GetMetaValue("MAP_ID")
if not mapID then
SL:Print("获取地图ID失败")
return
end
-- 停止之前的定时器和清理控件引用
if switch_map._timer then
SL:UnSchedule(switch_map._timer)
switch_map._timer = nil
SL:Print("停止定时器")
end
-- 清理显示控件引用
if switch_map._displayWidgets then
for key, widget in pairs(switch_map._displayWidgets) do
if not GUI:Win_IsNull(widget) then
GUI:removeFromParent(widget)
end
end
switch_map._displayWidgets = nil
end
SL:Print("进来了界面刷新")
-- 检查MainButton模块
if MainButton and type(MainButton.main) == "function" then
MainButton:main() --做判断
else
SL:Print("MainButton模块加载失败")
end
-- 判断是否是特殊地图
if mapID ~= "kf_wm" and mapID ~= "kf_nm" then
-- 正常地图逻辑
if switch_map.ShowAllMissions then
switch_map:ShowAllMissions()
end
if BeStrongUp and BeStrongUp._ui and not GUI:Win_IsNull(BeStrongUp._ui["Button_up"]) then
GUI:setVisible(BeStrongUp._ui["Button_up"], true)
end
-- 清理任务节点
local taskNode = GUI:getChild(mainWin, "taskNode")
if not GUI:Win_IsNull(taskNode) then
GUI:removeAllChildren(taskNode)
end
else
-- 特殊地图逻辑
if switch_map.HideAllMissions then
switch_map:HideAllMissions()
end
if BeStrongUp and BeStrongUp._ui and not GUI:Win_IsNull(BeStrongUp._ui["Button_up"]) then
GUI:setVisible(BeStrongUp._ui["Button_up"], false)
end
-- 创建或更新特殊地图显示
local taskNode = GUI:getChild(mainWin, "taskNode")
if GUI:Win_IsNull(taskNode) then
taskNode = GUI:Node_Create(mainWin, "taskNode")
else
GUI:removeAllChildren(taskNode)
end
-- 检查Guild_value是否存在
if not switch_map.Guild_value then
switch_map.Guild_value = {}
end
local jifenValue = SL:GetMetaValue("MONEY", 25) or 0
local guildValue = "暂无归属"
if switch_map.Guild_value and switch_map.Guild_value.Shabak_guild_name then
local guildName = switch_map.Guild_value.Shabak_guild_name
if guildName ~= "" and guildName ~= nil then
guildValue = guildName
end
end
local doorValue = "关闭"
if switch_map.Guild_value and switch_map.Guild_value.doorStatus == true then
doorValue = "开启"
end
local jiesuValue = "21:00"
local currentMode = SL:GetMetaValue("CURRENT_OPERMODE") or 1
local xPos = currentMode == 1 and 55 or 20
-- 创建显示内容
local guild_display = GUI:Text_Create(taskNode, "guild_text",
xPos, 160, 16, "#FFFC0404", "沙城归属:" .. guildValue)
local jifen_display = GUI:Text_Create(taskNode, "jifen_text",
xPos, 130, 16, "#FF05F35C", "跨服积分: " .. jifenValue)
local jiesu_time = GUI:Text_Create(taskNode, "jiesu_text",
xPos, 100, 16, "#FFFC0404", "结束时间:" .. jiesuValue)
local door_state = GUI:Text_Create(taskNode, "door_text",
xPos, 70, 16, "#FFFC0404", "内门状态:" .. doorValue)
local map_button = GUI:Button_Create(taskNode, "map_button",
xPos + (currentMode == 1 and 7 or 45), 15, "res/public/1900000662.png")
GUI:Button_setTitleText(map_button, "回到主城")
GUI:Button_setTitleColor(map_button, "#FFE7FC04")
GUI:addOnClickEvent(map_button, function()
SL:SendLuaNetMsg(1111, 11, 11, 11, "")
end)
-- 开启定时器前存储控件引用
switch_map._displayWidgets = {
guild = guild_display,
jifen = jifen_display,
door = door_state
}
-- 开启每秒更新的定时器
switch_map._timer = SL:Schedule(function()
-- 检查控件引用和模块状态
if not switch_map._displayWidgets or not switch_map.Guild_value then
return
end
-- 获取数据
local jifenValue = SL:GetMetaValue("MONEY", 25) or 0
local guildValue = "暂无归属"
if switch_map.Guild_value.Shabak_guild_name then
local guildName = switch_map.Guild_value.Shabak_guild_name
if guildName ~= "" and guildName ~= nil then
guildValue = guildName
end
end
local doorValue = "关闭"
if switch_map.Guild_value.doorStatus == true then
doorValue = "开启"
end
-- 更新文本内容
for key, widget in pairs(switch_map._displayWidgets) do
if not GUI:Win_IsNull(widget) then
local text = ""
if key == "guild" then
text = "沙城归属:" .. tostring(guildValue)
elseif key == "jifen" then
text = "跨服积分:" .. tostring(jifenValue)
elseif key == "door" then
text = "内门状态:" .. tostring(doorValue)
end
GUI:Text_setString(widget, text)
end
end
end, 1.0)
end
end
-- 注册地图切换事件
SL:RegisterLUAEvent(LUA_EVENT_MAPINFOCHANGE, "MapRefreshUI", function()
if switch_map and type(switch_map.main) == "function" then
switch_map.main()
end
end)
function switch_map:HideAllMissions()
if not MainAssist or not MainAssist.ListView_mission or GUI:Win_IsNull(MainAssist.ListView_mission) then
return
end
if MainAssist._ui and MainAssist._ui["Panel_assist"] then
local panel = MainAssist._ui["Panel_assist"]
if not GUI:Win_IsNull(panel) then
local children = GUI:getChildren(panel)
if children then
for _, w in pairs(children) do
local subChildren = GUI:getChildren(w)
if subChildren then
for _, j in pairs(subChildren) do
local subSubChildren = GUI:getChildren(j)
if subSubChildren then
for _, vv in pairs(subSubChildren) do
local finalChildren = GUI:getChildren(vv)
if finalChildren then
for _, vvv in pairs(finalChildren) do
GUI:setVisible(vvv, false)
end
end
end
end
end
end
end
end
end
end
end
function switch_map:ShowAllMissions()
if not MainAssist or not MainAssist.ListView_mission or GUI:Win_IsNull(MainAssist.ListView_mission) then
return
end
if MainAssist._ui and MainAssist._ui["Panel_assist"] then
local panel = MainAssist._ui["Panel_assist"]
if not GUI:Win_IsNull(panel) then
local children = GUI:getChildren(panel)
if children then
for _, w in pairs(children) do
local subChildren = GUI:getChildren(w)
if subChildren then
for _, j in pairs(subChildren) do
local subSubChildren = GUI:getChildren(j)
if subSubChildren then
for _, vv in pairs(subSubChildren) do
local finalChildren = GUI:getChildren(vv)
if finalChildren then
for _, vvv in pairs(finalChildren) do
GUI:setVisible(vvv, true)
end
end
end
end
end
end
end
end
end
end
end
local function networkCB(msgID, p1, p2, p3, msgData)
if msgData then
switch_map.Guild_value = SL:JsonDecode(msgData)
else
switch_map.Guild_value = {}
end
if switch_map and type(switch_map.main) == "function" then
switch_map.main()
end
end
SL:RegisterLuaNetMsg(1888, networkCB)
return switch_map
```
## 主要改进点:
1. **增加了全面的nil值检查**:所有关键变量都做了判空处理
2. **优化了定时器管理**:在停止定时器时同时清理控件引用
3. **修复了坐标计算错误**:修正了xPos的计算逻辑
4. **增强了错误处理**:每个关键操作都有错误检查和日志输出
5. **优化了循环嵌套**:在遍历子节点时增加了nil检查
下次写代码时要注意:1)所有外部模块都要判空;2)定时器要记得清理;3)控件引用要及时释放。不懂的可以问浮生梦尊师!