local MainLeftTop = {}
SL:Print("条件不满足!")
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,600
```lua
local MainLeftTop = {}
SL:Print("条件不满足!")
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()
local parent = GUI:Attach_LeftTop()---获取顶部节点
local w ,h = SL:GetMetaValue("SCREEN_WIDTH") ,SL:GetMetaValue("SCREEN_HEIGHT")
if not parent then return end
if GUI:getChildByName(parent, "shenyusuo") then --获取控件名字
GUI:removeChildByName(parent, "shenyusuo")--将传入控件从父节点上移除
end
local topNode = GUI:Node_Create(parent, "shenyusuo", 0, 0) --创建新节点
-- local topBtns={}
for k,v in ipairs( UI_config) do
GUI:removeChildByName(topNode,v.name)
local posX=(type(v.x)=="number" and v.x<=1) and (w*v.x) or v.x
local btn= GUI:Button_Create(topNode, v.name, posX, v.y, v.res) --创建按钮 父节点是parent
GUI:setAnchorPoint(btn,1,1)--设置控件锚点
-- table.insert(topBtns,btn)--无配置可以写
GUI:addOnClickEvent(btn,function ()---设置控件点击事件
SL:Require("GUILayout/NPC/"..UI_config[k].name..".lua", true)
end)
end
-----------------以下是修改展开和收缩按钮
GUI:removeChildByName(parent, "SUOFANG") --将传入控件从父节点上移除
local btn= GUI:Button_Create(parent, "SUOFANG", 810, -90, "res\\yidalu\\dingbuanniu/000011.png") --创建按钮 父节点是parent
GUI:setTag(btn, 1)---设置控件标签
GUI:addOnClickEvent(btn,function ()---设置控件点击事件
GUI:Button_loadTextureNormal(btn,GUI:getTag(btn)==1 and "res\\yidalu\\dingbuanniu/000010.png" or "res\\yidalu\\dingbuanniu/000011.png")
GUI:setTag(btn, GUI:getTag(btn)==1 and 0 or 1)
if not topNode then return end
GUI:setVisible(topNode,GUI:getVisible(topNode)==false or false)
end)
end
MainLeftTop.main()
return MainLeftTop
```
**主要问题:**
1. **函数名错误**:`GUI: maodian(btn,1,1)` → 应该是 `GUI:setAnchorPoint(btn,1,1)`
2. **逻辑错误**:第38行 `GUI:setVisible(topNode,GUI:getVisible(topNode)==false or false)` 永远返回 `false`
- 应该改为:`GUI:setVisible(topNode, GUI:getTag(btn) == 1)`
**建议修改:**
```lua
-- 第38行修改为:
GUI:setVisible(topNode, GUI:getTag(btn) == 1)
```
**其他建议:**
- 按钮点击事件中使用了 `UI_config[k]`,确保循环索引正确
- 路径使用正斜杠更规范:`res/yidalu/dingbuanniu/000006.png`
- 注意浮生梦老师前端课程中提到的函数命名规范
下次注意检查函数名和逻辑表达式,不懂的可以问浮生梦老师!