这段代码有几个问题需要修正:
**问题1:字符串拼接错误**
`"pos_"..i` 不能作为变量名,应该使用 `local pos = GUI:getPosition(...)`
**问题2:获取控件对象方式错误**
`"npc.ui.Effect_"..i` 返回的是字符串,而不是控件对象
**正确写法:**
```lua
for i = 1, 3 do
local effectWidget = npc.ui["Effect_" .. i]
if effectWidget and not GUI:Win_IsNull(effectWidget) then
local pos = GUI:getPosition(effectWidget)
SL:Print("Effect_" .. i .. " 坐标:x=" .. pos.x .. ", y=" .. pos.y)
end
end
```
**或者使用更简洁的方式:**
```lua
for i = 1, 3 do
local pos = GUI:getPosition(npc.ui["Effect_" .. i])
if pos then
SL:Print("坐标x:", pos.x, "坐标y:", pos.y)
end
end
```
**关键点:**
1. `npc.ui["Effect_" .. i]` 通过索引获取控件对象
2. 使用 `GUI:Win_IsNull()` 检查控件是否有效
3. `GUI:getPosition()` 返回包含 x 和 y 的 table