for i = 1, 2 do
local button = ui["Button_" .. i]
if button and not GUI:Win_IsNull(button) then
-- 当前选中的按钮,设置为发光图片
local glowPath = ""
if i == 1 then
glowPath = "res/
这段代码存在几个问题,我来帮你分析一下:
1. **逻辑错误**:代码中缺少了判断当前选中按钮的条件。你有一个 `if` 判断当前选中的按钮,但缺少了对应的 `else` 来处理其他按钮。
2. **结构不完整**:代码中只有一个 `if` 分支,没有 `else` 分支来区分选中和非选中状态。
3. **变量 `ui` 未定义**:代码中使用了 `ui["Button_" .. i]`,但 `ui` 变量没有定义。
下面是修正后的代码:
```lua
-- 假设 ui 是一个包含按钮控件的表
local ui = {} -- 这里需要你实际定义 ui 表
-- 假设当前选中的按钮索引
local selectedIndex = 1 -- 例如当前选中第一个按钮
for i = 1, 2 do
local button = ui["Button_" .. i]
if button and not GUI:Win_IsNull(button) then
if i == selectedIndex then
-- 当前选中的按钮,设置为发光图片
local glowPath = ""
if i == 1 then
glowPath = "res/yidalu/dengji tupo/bt1.png"
elseif i == 2 then
glowPath = "res/yidalu/dengji tupo/bt3.png"
end
if glowPath ~= "" then
GUI:Button_loadTextureNormal(button, glowPath)
GUI:Button_loadTexturePressed(button, glowPath)
end
else
-- 其他按钮,设置为原图片
local normalPath = ""
local pressedPath = ""
if i == 1 then
normalPath = "res/yidalu/dengji tupo/bt.png"
pressedPath = "res/yidalu/dengji tupo/bt1.png" -- 按压也用普通图片
elseif i == 2 then
normalPath = "res/yidalu/dengji tupo/bt2.png"
pressedPath = "res/yidalu/dengji tupo/bt3.png"
end
if normalPath ~= "" then
GUI:Button_loadTextureNormal(button, normalPath)
end
if pressedPath ~= "" then
GUI:Button_loadTexturePressed(button, pressedPath)
end
end
GUI:setVisible(button, true) -- 保持按钮可见
end
end
```
**主要修改:**
- 添加了 `selectedIndex` 变量来标识当前选中的按钮
- 添加了 `if i == selectedIndex then` 的判断条件
- 添加了对应的 `else` 分支来处理非选中状态
- 将 `GUI:setVisible(button, true)` 移到循环外部,避免重复调用
**函数说明:**
- `GUI:Win_IsNull(widget)`:判断控件是否为空
- `GUI:Button_loadTextureNormal(button, filepath)`:设置按钮正常状态图片
- `GUI:Button_loadTexturePressed(button, filepath)`:设置按钮按下状态图片
- `GUI:setVisible(widget, value)`:设置控件可见性
下次写代码时要注意逻辑结构的完整性,如果有不懂的地方可以随时问我!