MainRightTop = {}
local IconData = {
{
{ notes = "在线充值按钮", path = "res/custom/top/11.png" },
{ notes = "狂暴之力按钮", path = "res/custom/top/12.png" },
},
{
{ notes = "每日礼包按钮", path = "res/custom/top/21.png" }
},
{
}
}
--判断图标放入
function M
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-17 20:09:22 |
👁️ 阅读: 6
你的代码整体结构是正确的,但有几个关键问题需要修正:
1. **刷新逻辑问题**:`networkCB`函数中的`parent`变量未定义,会导致错误
2. **数据重复添加**:`judgeIcon`函数会重复向`IconData[3]`添加数据,每次刷新都会添加一次
3. **UI清理不彻底**:收起按钮事件中缺少对`IconData`的重新初始化
**修正后的代码**:
```lua
MainRightTop = {}
-- 图标数据定义为局部变量,避免全局污染
local IconData = {
{
{ notes = "在线充值按钮", path = "res/custom/top/11.png" },
{ notes = "狂暴之力按钮", path = "res/custom/top/12.png" },
},
{
{ notes = "每日礼包按钮", path = "res/custom/top/21.png" }
},
{}
}
-- 判断图标放入(需要重新调用时重新生成数据)
function MainRightTop.judgeIcon()
SL:Print("judge中")
-- 获取时间
local timeTable = os.date("*t")
-- 获取开服天数
local g2Value = SL:GetMetaValue("SERVER_VALUE", "G2")
g2Value = tonumber(g2Value) or 0
-- 清空第三行数据,避免重复添加
IconData[3] = {}
-- 第二行判断
if g2Value < 8 then
table.insert(IconData[3], { notes = "七日签到按钮", path = "res/custom/top/22.png" })
end
-- 第三行判断
if timeTable.year == 2026 and timeTable.month == 6 and timeTable.day > 18 and timeTable.day < 22 then
table.insert(IconData[3], { notes = "浓情端午按钮", path = "res/custom/top/31.png" })
end
end
-- 接受消息初始化图标数据
function networkCB(msgID, msgData)
if msgData == "刷新图标" then
-- 先获取父节点
local parent = GUI:Attach_RightTop()
if not GUI:Win_IsNull(parent) then
GUI:removeAllChildren(parent)
-- 重新调用main函数
MainRightTop.main()
end
end
end
SL:RegisterNetMsg(100, networkCB)
function MainRightTop.main()
local parent = GUI:Attach_RightTop()
-- 先判断图标数据
MainRightTop.judgeIcon()
local buttonLayout = GUI:Layout_Create(parent, "buttonLayout", -665, -265, 445, 235, true)
local topIcons = GUI:Layout_Create(buttonLayout, "topIcons", 0, 0, 445, 235, false)
GUI:setVisible(topIcons, true)
-- 初始化图标
MainRightTop.initIconData(topIcons)
-- 创建收起图标
local Button = GUI:Button_Create(parent, "Button", -215, -105, "res/custom/top/01.png")
-- 收起图标点击事件
GUI:addOnClickEvent(Button, function()
if GUI:getVisible(topIcons) == true then
GUI:Timeline_MoveTo(topIcons, { x = 445, y = 0 }, 0.2, function()
GUI:setVisible(topIcons, false)
GUI:Button_loadTextureNormal(Button, "res/custom/top/01.png")
end)
else
GUI:removeAllChildren(topIcons)
-- 重新判断图标数据
MainRightTop.judgeIcon()
-- 重新初始化图标
MainRightTop.initIconData(topIcons)
GUI:setVisible(topIcons, true)
GUI:Timeline_MoveTo(topIcons, { x = 0, y = 0 }, 0.2, function()
GUI:Button_loadTextureNormal(Button, "res/custom/top/01.png")
end)
end
end)
end
function MainRightTop.initIconData(topIcons)
local iconWidth = 80
local iconHeight = 80
local iconPaddingX = 10
-- 清空所有子节点(避免重复创建)
GUI:removeAllChildren(topIcons)
local tableLen1 = MainRightTop.getTableLen(IconData[1])
local tableLen2 = MainRightTop.getTableLen(IconData[2])
local tableLen3 = MainRightTop.getTableLen(IconData[3])
-- 循环创建一行图标
for col = 1, tableLen1 do
local iconX = 360 - (iconWidth + iconPaddingX) * (col - 1)
local iconY = iconHeight * 2
local path = IconData[1][col].path
local button = GUI:Button_Create(topIcons, "icon1_" .. col, iconX, iconY, path)
GUI:addOnClickEvent(button, function()
SL:SendNetMsg(99, 0, 0, 0, IconData[1][col].notes)
end)
end
-- 循环创建二行图标
for col = 1, tableLen2 do
local iconX = 360 - (iconWidth + iconPaddingX) * (col - 1)
local iconY = iconHeight
local path = IconData[2][col].path
local button = GUI:Button_Create(topIcons, "icon2_" .. col, iconX, iconY, path)
GUI:addOnClickEvent(button, function()
SL:SendNetMsg(99, 0, 0, 0, IconData[2][col].notes)
end)
end
-- 循环创建三行图标
for col = 1, tableLen3 do
local iconX = 360 - (iconWidth + iconPaddingX) * (col - 1)
local iconY = 0
local path = IconData[3][col].path
local button = GUI:Button_Create(topIcons, "icon3_" .. col, iconX, iconY, path)
GUI:addOnClickEvent(button, function()
SL:SendNetMsg(99, 0, 0, 0, IconData[3][col].notes)
end)
end
end
function MainRightTop.getTableLen(t)
local count = 0
for _ in pairs(t) do
count = count + 1
end
return count
end
```
**主要改进点**:
1. 在`networkCB`中正确获取`parent`变量
2. 在`judgeIcon`中清空`IconData[3]`避免重复添加
3. 在`initIconData`开始时清理所有子节点
4. 调整了函数调用顺序,确保数据先判断再创建
这样写就完全可行了!如果还有不懂的地方可以问浮生梦尊师!
← 返回问答库