local MainLeftTop = {}
local UI_config={
{name="shijieditu",x=0.3,y=-7,res="res\\yidalu\\dingbuanniu/000006.png",cmd={1234,6001}},
{name="gonglue",x=0.4,y=-7,res="res\\yidalu\\dingbuanniu/000008.png",cmd={1234,6002}},
}
function MainLeftTop
看了你的代码,整体思路不错,但有几个关键问题需要修正:
## 主要问题:
1. **坐标计算错误**:
```lua
local posX = (type(v.x) == "number" and v.x <= 1) and (w * v.x) or v.x
```
这个逻辑有问题,应该先判断是否为比例值(0-1),再计算绝对坐标
2. **函数使用不当**:
- `GUI:removeChildByName` 在知识库中未找到,应该使用 `GUI:removeFromParent`
- 创建节点建议使用 `GUI:Widget_Create` 而不是 `GUI:Node_Create`
3. **事件注册问题**:
`LUA_EVENT_ENTER_WORLD` 事件可能触发过早,建议使用更稳定的初始化方式
## 修正后的代码:
```lua
local MainLeftTop = {}
local UI_config = {
{name = "shijieditu", x = 0.3, y = -7, res = "res\\yidalu\\dingbuanniu/000006.png", cmd = {1234, 6001}},
{name = "gonglue", x = 0.4, y = -7, res = "res\\yidalu\\dingbuanniu/000008.png", cmd = {1234, 6002}},
}
function MainLeftTop.main()
-- 延迟执行,确保UI系统已初始化
SL:ScheduleOnce(function()
local parent = GUI:Attach_RightTop()
SL:Print("parent 值: ", parent)
if GUI:Win_IsNull(parent) then
SL:Print("错误: GUI:Attach_RightTop() 返回空值!")
return
end
local w = SL:GetMetaValue("SCREEN_WIDTH") or 800
local h = SL:GetMetaValue("SCREEN_HEIGHT") or 600
SL:Print("屏幕尺寸: ", w, "x", h)
-- 移除已存在的容器
local oldNode = GUI:getChildByName(parent, "shenyusuo")
if not GUI:Win_IsNull(oldNode) then
GUI:removeFromParent(oldNode)
end
-- 创建容器节点
local topNode = GUI:Widget_Create(parent, "shenyusuo", 0, 0, w, 100)
SL:Print("topNode 创建: ", topNode)
-- 创建配置中的按钮
for k, v in ipairs(UI_config) do
-- 计算坐标
local posX = v.x
if type(v.x) == "number" and v.x >= 0 and v.x <= 1 then
posX = w * v.x -- 比例值转为绝对坐标
end
SL:Print("创建按钮: ", v.name, " 位置X: ", posX, " 位置Y: ", v.y)
-- 移除已存在的同名按钮
local oldBtn = GUI:getChildByName(topNode, v.name)
if not GUI:Win_IsNull(oldBtn) then
GUI:removeFromParent(oldBtn)
end
-- 创建按钮
local btn = GUI:Button_Create(topNode, v.name, posX, v.y, v.res)
if not GUI:Win_IsNull(btn) then
GUI:setAnchorPoint(btn, 1, 1)
GUI:addOnClickEvent(btn, function()
-- 根据你的需求执行命令
if v.cmd and #v.cmd >= 2 then
SL:SendLuaNetMsg(v.cmd[1], v.cmd[2], 0, 0, "")
end
end)
else
SL:Print("创建按钮失败: ", v.name)
end
end
-- 创建缩放按钮
local oldBtn = GUI:getChildByName(parent, "SUOFANG")
if not GUI:Win_IsNull(oldBtn) then
GUI:removeFromParent(oldBtn)
end
local btn = GUI:Button_Create(parent, "SUOFANG", 810, -90, "res\\yidalu\\dingbuanniu/000011.png")
if not GUI:Win_IsNull(btn) then
GUI:setTag(btn, 1)
GUI:addOnClickEvent(btn, function()
local currentTag = GUI:getTag(btn)
local newTexture = (currentTag == 1) and "res\\yidalu\\dingbuanniu/000010.png" or "res\\yidalu\\dingbuanniu/000011.png"
GUI:Button_loadTextureNormal(btn, newTexture)
GUI:setTag(btn, (currentTag == 1) and 0 or 1)
if not GUI:Win_IsNull(topNode) then
local isVisible = GUI:getVisible(topNode)
GUI:setVisible(topNode, not isVisible)
end
end)
end
end, 1.0) -- 延迟1秒执行
end
-- 更稳定的初始化方式
local function init()
SL:ScheduleOnce(function()
MainLeftTop.main()
end, 0.5)
end
-- 注册初始化事件
SL:RegisterLUAEvent("LUA_EVENT_ENTER_WORLD", "MainLeftTop", init)
return MainLeftTop
```
## 改进点:
1. **使用 `GUI:Win_IsNull()`** 检查控件是否有效
2. **使用 `GUI:removeFromParent()`** 移除节点
3. **使用 `GUI:Widget_Create()`** 创建容器
4. **修正坐标计算逻辑**
5. **添加更详细的日志输出**
下次记得检查函数是否存在,不懂的可以问浮生梦老师!