-- 控制Button_1到Button_5的显示逻辑和图片切换
for i = 1, 5 do
local button = ui["Button_" .. i]
if button and not GUI:Win_IsNull(button) then
SL:print("按钮" .. i .. "的currentIndex是:" .. currentIndex)
if i == cur
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-03-21 12:51:04 |
👁️ 阅读: 5
从你的调试信息可以看到,所有按钮的 `currentIndex` 都是 3,这说明 `currentIndex` 变量没有正确更新。
问题出现在你的逻辑判断上:当 `i == currentIndex` 时(也就是 `i == 3` 时),第三个按钮会执行发光图片的逻辑。但是你看代码,你为所有按钮设置的发光图片路径都是同一个:
```lua
GUI:Button_loadTextureNormal(button, "res/moshenyuanli/baojifaguang.png")
GUI:Button_loadTexturePressed(button, "res/moshenyuanli/baojifaguang.png")
```
这意味着:
- 按钮1:`i=1`,`currentIndex=3`,`i != currentIndex`,执行 else 分支,设置原图片
- 按钮2:`i=2`,`currentIndex=3`,`i != currentIndex`,执行 else 分支,设置原图片
- 按钮3:`i=3`,`currentIndex=3`,`i == currentIndex`,执行 if 分支,设置发光图片
- 按钮4:`i=4`,`currentIndex=3`,`i != currentIndex`,执行 else 分支,设置原图片
- 按钮5:`i=5`,`currentIndex=3`,`i != currentIndex`,执行 else 分支,设置原图片
所以理论上只有按钮3会显示 `baojifaguang.png`。如果所有按钮都显示这个图片,可能有两个原因:
1. **图片路径问题**:检查一下你的图片文件是否存在,或者是否所有按钮的原图片路径都指向了同一个文件
2. **按钮对象获取问题**:确保 `ui["Button_" .. i]` 正确获取到了对应的按钮对象
建议你添加更详细的调试信息:
```lua
for i = 1, 5 do
local button = ui["Button_" .. i]
if button and not GUI:Win_IsNull(button) then
SL:print("按钮" .. i .. "的currentIndex是:" .. currentIndex)
SL:print("按钮" .. i .. "是否等于currentIndex:" .. (i == currentIndex))
if i == currentIndex then
SL:print("按钮" .. i .. "设置发光图片")
GUI:Button_loadTextureNormal(button, "res/moshenyuanli/baojifaguang.png")
GUI:Button_loadTexturePressed(button, "res/moshenyuanli/baojifaguang.png")
else
SL:print("按钮" .. i .. "设置原图片")
-- 原有逻辑...
end
else
SL:print("按钮" .. i .. "获取失败或为空")
end
end
```
这样能更清楚地看到每个按钮执行了哪个分支。另外,检查一下你的按钮点击事件是否正确更新了 `currentIndex` 变量。
从你的调试信息可以看到,所有按钮的 `currentIndex` 都是 3,这说明 `currentIndex` 变量没有正确更新。
问题出现在你的逻辑判断上:当 `i == currentIndex` 时(也就是 `i == 3` 时),第三个按钮会执行发光图片的逻辑。但是你看代码,你为所有按钮设置的发光图片路径都是同一个:
```lua
GUI:Button_loadTextureNormal(button, "res/moshenyuanli/baojifaguang.png")
GUI:Button_loadTexturePressed(button, "res/moshenyuanli/baojifaguang.png")
```
这意味着:
- 按钮1:`i=1`,`currentIndex=3`,`i != currentIndex`,执行 else 分支,设置原图片
- 按钮2:`i=2`,`currentIndex=3`,`i != currentIndex`,执行 else 分支,设置原图片
- 按钮3:`i=3`,`currentIndex=3`,`i == currentIndex`,执行 if 分支,设置发光图片
- 按钮4:`i=4`,`currentIndex=3`,`i != currentIndex`,执行 else 分支,设置原图片
- 按钮5:`i=5`,`currentIndex=3`,`i != currentIndex`,执行 else 分支,设置原图片
所以理论上只有按钮3会显示 `baojifaguang.png`。如果所有按钮都显示这个图片,可能有两个原因:
1. **图片路径问题**:检查一下你的图片文件是否存在,或者是否所有按钮的原图片路径都指向了同一个文件
2. **按钮对象获取问题**:确保 `ui["Button_" .. i]` 正确获取到了对应的按钮对象
建议你添加更详细的调试信息:
```lua
for i = 1, 5 do
local button = ui["Button_" .. i]
if button and not GUI:Win_IsNull(button) then
SL:print("按钮" .. i .. "的currentIndex是:" .. currentIndex)
SL:print("按钮" .. i .. "是否等于currentIndex:" .. (i == currentIndex))
if i == currentIndex then
SL:print("按钮" .. i .. "设置发光图片")
GUI:Button_loadTextureNormal(button, "res/moshenyuanli/baojifaguang.png")
GUI:Button_loadTexturePressed(button, "res/moshenyuanli/baojifaguang.png")
else
SL:print("按钮" .. i .. "设置原图片")
-- 原有逻辑...
end
else
SL:print("按钮" .. i .. "获取失败或为空")
end
end
```
这样能更清楚地看到每个按钮执行了哪个分支。另外,检查一下你的按钮点击事件是否正确更新了 `currentIndex` 变量。
← 返回问答库