我正在尝试使用 ComputerCraft mod 在我的世界中创建一个 Shop-Keep。目前,我将三个选项传递到我的函数中,但是当我尝试选择一个选项时,只有第一个选项正确地打破了循环。我想让玩家选择和选项两次才能完成,这样如果他们点击错误,他们就会改变他们的选择。目前,只有第一个选项更新。
function UTOptions(opt)
local FR = 0
local max = 14
local w,h = term.getSize()
local check = {}
while true do
for i,v in ipairs(opt) do
if(string.len(v)>max) then
error("Option is too long.")
end
if (FR == 0) then
check[i] = 0
end
term.setCursorPos(w-15,i+2)
if (check[i]== 1) then
term.setTextColor(colors.red)
write("*")
term.setTextColor(colors.white)
write(v)
else
write("*"..v)
end
end
local event, side, mx, my = os.pullEvent("monitor_touch")
if (check[my-2] == 0) then
check[my-2] = 1
for i,v in ipairs(check) do
if (v ~= my-2) then
check[i] = 0
end
end
elseif (check[my-2]==1) then
return my-2
end
FR = 1
end
end
我尝试更改中的值
local event, side, mx, my = os.pullEvent("monitor_touch") if (check[my-2] == 0) then...
例如使用 my-3 和 my-4,但这只会改变哪个选项有效,其他选项仍然不会更新。我感觉这个错误与这部分有关:
for i,v in ipairs(check) do
if (v ~= my-2) then
check[i] = 0
end
end
我找不到更好的方法将所有未选中的选项更新为0。
问题在于我对 for 循环的理解。在这个块中,“v”需要是“i”,因为我想检查索引而不是值。 旧:
for i,v in ipairs(check) do
if (v ~= my-2) then
check[i] = 0
end
end
新:
for i,v in ipairs(check) do
if (i ~= my-2) then
check[i] = 0
end
end